-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.ml
More file actions
30 lines (22 loc) · 1.14 KB
/
day3.ml
File metadata and controls
30 lines (22 loc) · 1.14 KB
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
type instruction = Do|Dont|Mul of int * int
let inst_r = Str.regexp {|\(do()\|don't()\|mul(\([0-9]+\),\([0-9]+\))\)|}
let rec get_all_matches s i =
match Str.search_forward inst_r s i with
| j -> let instruction =
let s0 = (Str.matched_group 0 s) in
if s0 = "do()" then Do
else if s0 = "don't()" then Dont
else let a1 = Str.matched_group 2 s|>int_of_string in
let a2 = Str.matched_group 3 s|>int_of_string in
Mul(a1,a2)
in instruction::get_all_matches s (j + 1)
| exception Not_found -> []
let part1 s =
get_all_matches s 0|>List.fold_left (fun acc inst -> match inst with
| Mul(x, y) -> acc + x * y
| _ -> acc) 0
let part2 s =
get_all_matches s 0|>List.fold_left (fun (m,acc) inst -> match inst with
| Mul(x, y) -> m,(acc + x * y*m)
| Dont -> 0, acc
| Do -> 1, acc) (1,0) |> snd