-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathday02.ml
More file actions
33 lines (27 loc) · 776 Bytes
/
day02.ml
File metadata and controls
33 lines (27 loc) · 776 Bytes
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
let numbers =
CCIO.(with_in "inputs/02.txt" read_lines_l)
|> List.map CCFun.(String.split_on_char '\t' %> List.map int_of_string)
let diff row =
let max_val = List.fold_left max min_int row in
let min_val = List.fold_left min max_int row in
max_val - min_val
let rec find_divisors row =
let are_divisible a b =
a mod b = 0 || b mod a = 0 in
let divide a b =
if a mod b = 0 then a/b else b/a
in
match row with
| [] -> 0
| h :: t ->
match List.find_opt (are_divisible h) t with
| Some x -> divide h x
| None -> find_divisors t
let solve ~part =
let func = if part = 1 then diff else find_divisors in
numbers
|> List.fold_left (fun acc x -> func x + acc) 0
|> Printf.printf "%d\n"
let () =
solve ~part:1;
solve ~part:2