challenge-codinggame/the-bridge/code.ml
Glenn Y. Rolland 44f48dd8ab Initial import.
2016-07-26 14:21:15 +02:00

86 lines
2.8 KiB
OCaml

(* Auto-generated code below aims at helping you parse *)
(* the standard input according to the problem statement. *)
let open Printf in
let road = int_of_string (input_line stdin) in (* the length of the road before the gap. *)
let gap = int_of_string (input_line stdin) in (* the length of the gap. *)
let platform = int_of_string (input_line stdin) in (* the length of the landing platform. *)
(* compute minimum speed value to be able to jump *)
(* la distance dont j'ai encore besoin pour accelerer *)
let rec min_jump_distance_fn speed gap =
if speed <= gap then
(* je devrai accelerer *)
speed + (min_jump_distance_fn (speed + 1) gap)
else
(* pas besoin d'accelerer *)
speed
in
(* compute maximum speed value to be able to land *)
let rec landing_distance_fn speed =
if speed > 0 then
speed + (landing_distance_fn (speed - 1))
else
0
in
(* we must increase speed to be able to jump *)
(* we must reduce speed to be able to land *)
(* game loop *)
while true do
let speed = int_of_string (input_line stdin) in (* the motorbike's speed. *)
let coordx = int_of_string (input_line stdin) in (* the position on the road of the motorbike. *)
let gap_passed = coordx >= (road + gap) in
let road_remaining = road - coordx in
let min_jump_distance = min_jump_distance_fn speed gap in
let landing_distance = landing_distance_fn speed in
fprintf stderr "remaining = %d\n%!" road_remaining ;
fprintf stderr "gap = %d\n%!" gap ;
fprintf stderr "speed, gap_passed = %d, %B\n%!" speed gap_passed ;
fprintf stderr "r <= mjd = %B\n%!" (road_remaining <= min_jump_distance) ;
let must_speed = match (speed, gap_passed) with
| (0, false) -> true
| (_, false) -> road_remaining <= min_jump_distance
| (_, true) -> false
in
let must_slow = match (speed, gap_passed) with
| (_, true) -> true
| (0, false) -> false
| (_, false) -> (landing_distance >= platform)
in
let must_jump = match gap_passed with
| false -> (road_remaining > 0) && (road_remaining < speed)
| true -> false
in
fprintf stderr "min_jump_dist = %d\n%!" min_jump_distance ;
fprintf stderr "must_speed = %B\n%!" must_speed ;
fprintf stderr "must_slow = %B\n%!" must_slow ;
fprintf stderr "must_jump = %B\n%!" must_jump ;
let action = match (must_speed, must_slow, must_jump) with
(* we jump when we need to *)
| (_, _, true) -> "JUMP"
(* slow down to be able to land *)
| (_, true, _) -> "SLOW"
(* speed up to be able to jump *)
| (true, _, _) -> "SPEED"
(* otherwise *)
| (_, _, _) -> "WAIT"
in
(* Write an action using print_endline *)
(* To debug: prerr_endline "Debug message"; *)
print_endline action;
();
done;