challenge-codinggame/coders-strike-back/code3.ml
Glenn Y. Rolland 23b6693ef3 Up.
2016-07-28 02:44:57 +02:00

120 lines
2.7 KiB
OCaml

let open Printf in
let debug = fprintf stderr in
let checkpoint_ray = 600 in
let module Vector = struct
type t = {
x : int;
y : int
}
let create x y =
{ x ; y }
let diff dst src =
{ x = (dst.x - src.x) ;
y = (dst.y - src.y)
}
let mult src a =
{ x = src.x * a ;
y = src.y * a
}
let add dst src =
{ x = (src.x + dst.x) ;
y = (src.y + dst.y)
}
let distance (dst:t) (src:t) =
(src.x - dst.x) * (src.x - dst.x) + (src.y - dst.y) * (src.y - dst.y)
|> float_of_int
|> sqrt
|> int_of_float
let is_near dst src =
(distance dst src) < (checkpoint_ray * 2)
let is_far dst src =
((distance dst src) >= checkpoint_ray * 6)
end
in
let module Pod = struct
type t = {
pos : Vector.t ;
dir : Vector.t ;
}
end
in
let parse_line1 () =
let line = input_line stdin in
let build_results_fn = (
fun x y checkpointx checkpointy checkpointdist checkpointangle ->
(Vector.create x y),
(Vector.create checkpointx checkpointy),
checkpointdist,
checkpointangle
) in
Scanf.sscanf line "%d %d %d %d %d %d" build_results_fn
in
let parse_line2 () =
let line = input_line stdin in
let build_results_fn = (
fun opponentx opponenty ->
(Vector.create opponentx opponenty)
) in
Scanf.sscanf line "%d %d" build_results_fn
in
let map_center = Vector.create (16000 / 2) (9000 / 2) in
let first_turn = ref true in
let old_pod = ref (Vector.create 0 0) in
let tick = ref 0 in
(* game loop *)
while true do
(* nextcheckpointdist: distance to the next checkpoint *)
(* nextcheckpointangle: angle between your pod orientation and the direction of the next checkpoint *)
let pod, checkpoint, checkpoint_dist, checkpoint_angle = parse_line1 () in
let opponent = parse_line2 () in
if !first_turn then begin
old_pod := pod ;
first_turn := false
end ;
(* output "x y thrust" : the target position + the power *)
let is_aligned = checkpoint_angle > -5 && checkpoint_angle < 5 in
let is_near_aligned = checkpoint_angle > -15 && checkpoint_angle < 15 in
let is_wide_aligned = checkpoint_angle > -30 && checkpoint_angle < 30 in
let is_aside = checkpoint_angle > -60 || checkpoint_angle < 60 in
let is_behind = checkpoint_angle >= 85 || checkpoint_angle <= -85 in
let use_boost = (!tick > 100) && is_aligned && (Vector.is_far pod checkpoint) in
debug "angle = %d\n%!" checkpoint_angle ;
let thrust =
(* droit devant *)
if is_behind then 6
else 100
in
let power_str = match use_boost with
| false -> string_of_int thrust
| true -> "BOOST"
in
printf "%d %d %s\n%!" checkpoint.x checkpoint.y power_str ;
tick := !tick + 1 ;
old_pod := pod ;
done;