-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.ml
46 lines (40 loc) · 1.04 KB
/
day07.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
let valid_equation operators numbers =
let test_value = List.hd numbers in
let numbers = List.tl numbers in
let rec f cur nums =
match nums with
| [] -> cur = test_value
| x :: xs ->
Option.is_some (List.find_opt (fun op -> f (op cur x) xs) operators)
in
f (List.hd numbers) (List.tl numbers)
let part1 lines =
lines
|> List.map Lib.extract_numbers
|> List.filter (valid_equation [ Int.add; Int.mul ])
|> List.map List.hd |> Lib.sum |> string_of_int
let concatenation a b = int_of_string (string_of_int a ^ string_of_int b)
let part2 lines =
lines
|> List.map Lib.extract_numbers
|> List.filter (valid_equation [ Int.add; Int.mul; concatenation ])
|> List.map List.hd |> Lib.sum |> string_of_int
let example =
Lib.parse_lines
{|
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
|}
let%expect_test _ =
print_string (part1 example);
[%expect {| 3749 |}]
let%expect_test _ =
print_string (part2 example);
[%expect {| 11387 |}]