-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.fs
More file actions
40 lines (30 loc) · 875 Bytes
/
Day03.fs
File metadata and controls
40 lines (30 loc) · 875 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
34
35
36
37
38
39
40
module Day03
open System.Text.RegularExpressions
type Ins =
| Mul of int * int
| Do
| Dont
let decode (a: GroupCollection) =
match a.[0].Value with
| s when s.StartsWith "mul" -> Mul(a.[1].Value |> int, a.[2].Value |> int)
| s when s.StartsWith "don" -> Dont
| s when s.StartsWith "do" -> Do
let exp = @"mul\((?<l>\d+),(?<r>\d+)\)|do.*?\(\)"
let parse =
Seq.map (fun s ->
Regex.Matches(s, exp)
|> Seq.map (fun m -> decode m.Groups))
>> Seq.concat
let simple =
function
| Mul (l, r) -> l * r
| _ -> 0
let part1 = Seq.sumBy simple
let conditional (a, inc) i =
match i with
| Mul (l, r) when inc -> (a + (l * r), inc)
| Do -> (a, true)
| Dont -> (a, false)
| _ -> (a, inc)
let part2 = Seq.fold conditional (0, true) >> fst
let Solve (xs: string seq) = xs |> parse |> both part1 part2