Skip to content

Commit ac567b1

Browse files
committed
Add 2024, day 3, part 2
1 parent 2460e37 commit ac567b1

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Year2024/Sources/Day03.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,40 @@ public enum Day03: Day {
66

77
public static let title = "Mull It Over"
88

9-
private static let regex = Regex {
9+
private static let regex1 = Regex {
1010
"mul("
1111
TryCapture.integer
1212
","
1313
TryCapture.integer
1414
")"
1515
}
1616

17+
private static let regex2 = Regex {
18+
ChoiceOf {
19+
regex1
20+
"do()"
21+
"don't()"
22+
}
23+
}
24+
1725
public static func part1(_ input: Input) throws -> Int {
18-
input.string.matches(of: regex).map { match in
26+
input.string.matches(of: regex1).map { match in
1927
match.output.1 * match.output.2
2028
}
2129
.sum
2230
}
2331

2432
public static func part2(_ input: Input) throws -> Int {
25-
0
33+
var active = true
34+
var total = 0
35+
for match in input.string.matches(of: regex2) {
36+
switch (match.output.0, active) {
37+
case ("do()", _): active = true
38+
case ("don't()", _): active = false
39+
case (_, false): continue
40+
default: total += match.output.1! * match.output.2!
41+
}
42+
}
43+
return total
2644
}
2745
}

Year2024/Tests/Day03Tests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ final class Day03Tests: XCTestCase {
1717
}
1818

1919
func testPart2Examples() throws {
20-
XCTAssertEqual(try Day03.part2([]), 0)
20+
XCTAssertEqual(try Day03.part2([
21+
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
22+
]), 48)
2123
}
2224

2325
func testPart2Puzzle() throws {
2426
let input = try Bundle.module.input(named: "Day03")
25-
XCTAssertEqual(try Day03.part2(input), 0)
27+
XCTAssertEqual(try Day03.part2(input), 103811193)
2628
}
2729
}

0 commit comments

Comments
 (0)