-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.ml
55 lines (48 loc) · 5.48 KB
/
day08.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
let input = "rect 1x1\nrotate row y=0 by 7\nrect 1x1\nrotate row y=0 by 5\nrect 1x1\nrotate row y=0 by 5\nrect 1x1\nrotate row y=0 by 2\nrect 1x1\nrotate row y=0 by 3\nrect 1x1\nrotate row y=0 by 5\nrect 1x1\nrotate row y=0 by 3\nrect 1x1\nrotate row y=0 by 2\nrect 1x1\nrotate row y=0 by 3\nrect 2x1\nrotate row y=0 by 7\nrect 6x1\nrotate row y=0 by 3\nrect 2x1\nrotate row y=0 by 2\nrect 1x2\nrotate row y=1 by 10\nrotate row y=0 by 3\nrotate column x=0 by 1\nrect 2x1\nrotate column x=20 by 1\nrotate column x=15 by 1\nrotate column x=5 by 1\nrotate row y=1 by 5\nrotate row y=0 by 2\nrect 1x2\nrotate row y=0 by 5\nrotate column x=0 by 1\nrect 4x1\nrotate row y=2 by 15\nrotate row y=0 by 5\nrotate column x=0 by 1\nrect 4x1\nrotate row y=2 by 5\nrotate row y=0 by 5\nrotate column x=0 by 1\nrect 4x1\nrotate row y=2 by 10\nrotate row y=0 by 10\nrotate column x=8 by 1\nrotate column x=5 by 1\nrotate column x=0 by 1\nrect 9x1\nrotate column x=27 by 1\nrotate row y=0 by 5\nrotate column x=0 by 1\nrect 4x1\nrotate column x=42 by 1\nrotate column x=40 by 1\nrotate column x=22 by 1\nrotate column x=17 by 1\nrotate column x=12 by 1\nrotate column x=7 by 1\nrotate column x=2 by 1\nrotate row y=3 by 10\nrotate row y=2 by 5\nrotate row y=1 by 3\nrotate row y=0 by 10\nrect 1x4\nrotate column x=37 by 2\nrotate row y=3 by 18\nrotate row y=2 by 30\nrotate row y=1 by 7\nrotate row y=0 by 2\nrotate column x=13 by 3\nrotate column x=12 by 1\nrotate column x=10 by 1\nrotate column x=7 by 1\nrotate column x=6 by 3\nrotate column x=5 by 1\nrotate column x=3 by 3\nrotate column x=2 by 1\nrotate column x=0 by 1\nrect 14x1\nrotate column x=38 by 3\nrotate row y=3 by 12\nrotate row y=2 by 10\nrotate row y=0 by 10\nrotate column x=7 by 1\nrotate column x=5 by 1\nrotate column x=2 by 1\nrotate column x=0 by 1\nrect 9x1\nrotate row y=4 by 20\nrotate row y=3 by 25\nrotate row y=2 by 10\nrotate row y=0 by 15\nrotate column x=12 by 1\nrotate column x=10 by 1\nrotate column x=8 by 3\nrotate column x=7 by 1\nrotate column x=5 by 1\nrotate column x=3 by 3\nrotate column x=2 by 1\nrotate column x=0 by 1\nrect 14x1\nrotate column x=34 by 1\nrotate row y=1 by 45\nrotate column x=47 by 1\nrotate column x=42 by 1\nrotate column x=19 by 1\nrotate column x=9 by 2\nrotate row y=4 by 7\nrotate row y=3 by 20\nrotate row y=0 by 7\nrotate column x=5 by 1\nrotate column x=3 by 1\nrotate column x=2 by 1\nrotate column x=0 by 1\nrect 6x1\nrotate row y=4 by 8\nrotate row y=3 by 5\nrotate row y=1 by 5\nrotate column x=5 by 1\nrotate column x=4 by 1\nrotate column x=3 by 2\nrotate column x=2 by 1\nrotate column x=1 by 3\nrotate column x=0 by 1\nrect 6x1\nrotate column x=36 by 3\nrotate column x=25 by 3\nrotate column x=18 by 3\nrotate column x=11 by 3\nrotate column x=3 by 4\nrotate row y=4 by 5\nrotate row y=3 by 5\nrotate row y=2 by 8\nrotate row y=1 by 8\nrotate row y=0 by 3\nrotate column x=3 by 4\nrotate column x=0 by 4\nrect 4x4\nrotate row y=4 by 10\nrotate row y=3 by 20\nrotate row y=1 by 10\nrotate row y=0 by 10\nrotate column x=8 by 1\nrotate column x=7 by 1\nrotate column x=6 by 1\nrotate column x=5 by 1\nrotate column x=3 by 1\nrotate column x=2 by 1\nrotate column x=1 by 1\nrotate column x=0 by 1\nrect 9x1\nrotate row y=0 by 40\nrotate column x=44 by 1\nrotate column x=35 by 5\nrotate column x=18 by 5\nrotate column x=15 by 3\nrotate column x=10 by 5\nrotate row y=5 by 15\nrotate row y=4 by 10\nrotate row y=3 by 40\nrotate row y=2 by 20\nrotate row y=1 by 45\nrotate row y=0 by 35\nrotate column x=48 by 1\nrotate column x=47 by 5\nrotate column x=46 by 5\nrotate column x=45 by 1\nrotate column x=43 by 1\nrotate column x=40 by 1\nrotate column x=38 by 2\nrotate column x=37 by 3\nrotate column x=36 by 2\nrotate column x=32 by 2\nrotate column x=31 by 2\nrotate column x=28 by 1\nrotate column x=23 by 3\nrotate column x=22 by 3\nrotate column x=21 by 5\nrotate column x=20 by 1\nrotate column x=18 by 1\nrotate column x=17 by 3\nrotate column x=13 by 1\nrotate column x=10 by 1\nrotate column x=8 by 1\nrotate column x=7 by 5\nrotate column x=6 by 5\nrotate column x=5 by 1\nrotate column x=3 by 5\nrotate column x=2 by 5\nrotate column x=1 by 5"
type op = Rect | RotRow | RotCol
let parse_line s =
let op_type =
if String.sub s 0 4 = "rect" then Rect
else if String.sub s 7 3 = "row" then RotRow
else RotCol
in
match s |> Str.split (Str.regexp "[a-z =]+") |> List.map int_of_string with
| [a;b] -> (op_type, a, b)
| _ -> failwith "unreachable"
let apply_op board (o,a,b) =
let () = match o with
| Rect ->
for y = 0 to (b-1) do
Array.fill board.(y) 0 a true
done
| RotRow ->
let row = Array.copy board.(a) in
for i = 0 to 49 do
let idx = (i + b) mod 50 in
board.(a).(idx) <- row.(i)
done
| RotCol ->
let col = Array.init 6 (fun i -> board.(i).(a)) in
for i = 0 to 5 do
let idx = (i + b) mod 6 in
board.(idx).(a) <- col.(i)
done
in
board
let board_to_str board =
let row_to_str r = r
|> Array.map (fun on -> if on then "#" else " ")
|> Array.to_list
|> String.concat ""
in
let str = board |> Array.map row_to_str |> Array.to_list |> String.concat "\n" in
"\n" ^ str
let count_tiles board =
let sum_row r = r |> Array.map Bool.to_int |> Array.fold_left (+) 0 in
board |> Array.map sum_row |> Array.fold_left (+) 0
let main () =
let ops = input |> Aoc.parse_lines parse_line in
let board = ops |> List.fold_left apply_op (Array.make_matrix 6 50 false) in
let part1 = count_tiles board |> string_of_int in
(part1, board_to_str board)
let () = Aoc.timer main