Skip to content

Instantly share code, notes, and snippets.

@zindel
Created December 14, 2019 18:36
Show Gist options
  • Save zindel/f079603103609d288393932e1185df66 to your computer and use it in GitHub Desktop.
Save zindel/f079603103609d288393932e1185df66 to your computer and use it in GitHub Desktop.
let run tiles program =
let tiles = ref tiles in
let score = ref 0L in
let prev_ball = ref (find Ball !tiles) in
let x_pad_desired, y_pad = find HPaddle !tiles in
let x_pad_desired = ref x_pad_desired in
let update_ball_line (x2, y2) =
let (x1, y1) = !prev_ball in
printf "BALL: (%Ld, %Ld) => (%Ld, %Ld)\n" x1 y1 x2 y2;
printf "(Y = %Ld) XPAD: %Ld => " y_pad !x_pad_desired;
let () =
match compare (x1, y1) (x2, y2) with
| 0 -> ()
| _ ->
let x_step = x2 - x1 in
x_pad_desired := x1 + x_step * (Int64.abs (y2 - y_pad));
in
prev_ball := (x2, y2);
printf "XPAD: %Ld \n" !x_pad_desired;
in
let get_input () =
printf "\n";
print_game !tiles;
let step = compare !x_pad_desired (find HPaddle !tiles |> fst) in
printf "JOYSTICK: %d\n" step;
Int64.of_int step
in
let state = Intcode.init get_input program in
let rec run () =
match Intcode.run_till_output state with
| `Halted ->
printf "Part2 = %Ld\n" !score;
print_game !tiles
| `Output ->
let x = Intcode.consume_output state in
match Intcode.run_till_output state with
| `Halted -> failwith "no second input"
| `Output ->
let y = Intcode.consume_output state in
match Intcode.run_till_output state with
| `Halted -> failwith "no third input"
| `Output ->
let () =
match x,y with
| -1L, 0L -> score := Intcode.consume_output state
| _ ->
let block = match Intcode.consume_output state with
| 0L -> Empty
| 1L -> Wall
| 2L -> Block
| 3L -> HPaddle
| 4L ->
update_ball_line (x, y);
Ball
| n -> failwith @@ sprintf "Unknown block %Ld" n
in
tiles := M.add (x, y) block !tiles;
in
run ()
in
run ();
!tiles
in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment