-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.fs
40 lines (32 loc) · 935 Bytes
/
03.fs
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
open System
open System.IO
open System.Text.RegularExpressions
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let text = File.ReadAllText(@"input")
let ans1 =
Regex.Matches(text, "mul\\([0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]?\\)")
|> Array.ofSeq
|> Array.sumBy (fun x ->
x.Value[4 .. x.Value.Length - 2].Split(",")
|> Array.map (int)
|> fun y -> y[0] * y[1])
let ans2 =
(true,
Regex.Matches(text, "(mul\\([0-9][0-9]?[0-9]?,[0-9][0-9]?[0-9]?\\)|don't\\(\\)|do\\(\\))")
|> Array.ofSeq)
||> Array.mapFold (fun d x ->
match x.Value with
| "do()" -> 0, true
| "don't()" -> 0, false
| _ when d ->
(x.Value[4 .. x.Value.Length - 2].Split(",")
|> Array.map (int)
|> fun y -> y[0] * y[1]),
d
| _ -> 0, d)
|> fun (a, b) -> Array.sum (a)
printfn "%A %A" ans1 ans2