-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.ml
49 lines (42 loc) · 1.17 KB
/
Day10.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
open Printf
let max_jump = 3;;
let rev_lines =
let lines = ref [] in
try
while true; do
lines := input_line stdin :: !lines
done;
!lines
with End_of_file -> !lines;;
let adapters = List.map int_of_string rev_lines |> List.sort compare;;
let chain = 0 :: adapters @ [3 + List.hd (List.rev adapters)];;
let first =
let left = List.rev chain |> List.tl |> List.rev in
let right = List.tl chain in
let diffs = List.map2 (-) right left in
let find n = List.filter (fun a -> a == n) diffs |> List.length in
find 1 * find 3;;
let ways = ref [1];;
let second =
let rec sum = function
| [] -> 0
| h::t -> h + (sum t)
in
let rec index_list index acc =
if acc == max_jump then []
else
let tail_rec = index_list (index + 1) (acc + 1) in
if index < 0 then tail_rec
else index :: tail_rec
in
let chain_iter index el =
ways := !ways @ [
index_list (index - max_jump + 1) 0
|> List.filter (fun it -> el - List.nth chain it <= max_jump)
|> List.map (List.nth !ways)
|> sum
]
in
List.tl chain |> List.iteri chain_iter;
List.rev !ways |> List.hd;;
Printf.printf "%d %d\n" first second;;