-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.ml
57 lines (48 loc) · 1.4 KB
/
day13.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
open CCFun
module Tile = struct
type t = Empty | Wall | Block | Paddle | Ball
let of_int = function
| 0 -> Empty
| 1 -> Wall
| 2 -> Block
| 3 -> Paddle
| 4 -> Ball
| _ -> failwith "invalid tile"
end
let count_blocks =
let rec aux blocks comp =
if comp |> Intcode.is_out_empty then blocks
else
let (_, _, t) = comp |> Intcode.get_next_3_outputs in
let blocks = blocks + CCBool.to_int (Tile.of_int t = Tile.Block) in
comp |> aux blocks
in
Intcode.run_until_halt %> aux 0
let play =
let paddle_pos = ref 0 in
let ball_pos = ref 0 in
let score = ref 0 in
let rec next_move comp =
match Intcode.get_state comp with
| Halted -> !score
| _ ->
let comp = comp |> Intcode.run_until_halt in
while not (Intcode.is_out_empty comp) do
let (x, y, t) = comp |> Intcode.get_next_3_outputs in
if (x, y) = (-1, 0) then score := t
else
match Tile.of_int t with
| Tile.Paddle -> paddle_pos := x
| Tile.Ball -> ball_pos := x
| _ -> ()
done;
let joystick = Int.compare !ball_pos !paddle_pos in
comp |> Intcode.receive joystick |> next_move
in
Intcode.set_positions [ (0, 2) ] %> next_move
let game =
Intcode.read_instructions "inputs/13.txt"
|> Intcode.initialize_computer
let () =
game |> count_blocks |> Printf.printf "%d\n";
game |> play |> Printf.printf "%d\n"