-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03.swift
51 lines (45 loc) · 1.26 KB
/
Day03.swift
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
47
48
49
50
51
import AOCCore
import Foundation
import RegexBuilder
struct Day03: Day {
let title = "Mull It Over"
var rawInput: String?
func part1() throws -> Int {
let query = Regex {
"mul("
TryCapture.integer
","
TryCapture.integer
")"
}
return input().raw
.matches(of: query)
.map { $0.output.1 * $0.output.2 }
.sum
}
func part2() throws -> Int {
let query = Regex {
ChoiceOf {
"do()"
"don't()"
Regex {
"mul("
TryCapture.integer
","
TryCapture.integer
")"
}
}
}
return input().raw.matches(of: query)
.reduce(into: (answer: 0, isEnabled: true)) { result, match in
switch (match.output.0, result.isEnabled) {
case ("do()", _): result.isEnabled = true
case ("don't()", _): result.isEnabled = false
case (_, false): return
default: result.answer += (match.output.1 ?? 0) * (match.output.2 ?? 0)
}
}
.answer
}
}