-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday11.ml
27 lines (24 loc) · 819 Bytes
/
day11.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
let ( <+> ) (p1, q1) (p2, q2) = (p1 + p2, q1 + q2)
let distance_to_origin (p, q) =
max (max (abs p) (abs q)) (abs (p + q))
let solution =
CCIO.(with_in "inputs/11.txt" read_all)
|> String.split_on_char ','
|> List.fold_left
(fun (pos, furthest) instruction ->
let direction =
match instruction with
| "n" -> ( 0, -1)
| "nw" -> (-1, 0)
| "sw" -> (-1, 1)
| "s" -> ( 0, 1)
| "se" -> ( 1, 0)
| "ne" -> ( 1, -1)
| _ -> failwith "impossible"
in
let pos' = pos <+> direction in
let furthest' = max furthest (distance_to_origin pos') in
(pos', furthest'))
((0, 0), 0)
|> fun (pos, furthest) -> (distance_to_origin pos, furthest)
let () = Printf.printf "%d\n%d\n" (fst solution) (snd solution)