Skip to content

Commit 9e64df2

Browse files
committed
day3
1 parent cd6c74a commit 9e64df2

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Below is my current advancement in Advent of code:
1010
|:---|:---:|:---:|
1111
| Day 1 | :star: | :star: |
1212
| Day 2 | :star: | :star: |
13-
| Day 3 | | |
13+
| Day 3 | :star: | :star: |
1414
| Day 4 | | |
1515
| Day 5 | | |
1616
| Day 6 | | |

Sources/Day03.swift

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@ import Algorithms
33
struct Day03: AdventDay {
44
var data: String
55

6-
func part1() -> Int {
7-
let search = /mul\((\d+),(\d+)\)/
8-
9-
return data.ranges(of: search).map { match in
10-
let string = data[match].trimmingPrefix("mul(").trimmingCharacters(in: .punctuationCharacters)
11-
let numbers = string.split(
12-
separator: ","
13-
)
14-
return numbers.map {
15-
Int($0)
16-
}.compactMap {
17-
$0
18-
}.reduce(1, *)
6+
/// computes the operations from a corrupter memory dump
7+
func compute(data input: String) -> Int {
8+
input.ranges(of: /mul\(\d+,\d+\)/).map { match in
9+
input[match].trimmingPrefix("mul(").trimmingCharacters(in: .punctuationCharacters)
10+
.split(
11+
separator: ","
12+
)
13+
.map {
14+
Int($0)
15+
}.compactMap {
16+
$0
17+
}.reduce(1, *)
1918
}.sum
2019
}
2120

21+
func part1() -> Int {
22+
compute(data: data)
23+
}
24+
2225
func part2() -> Int {
23-
0
26+
let singleLine = data.replacingOccurrences(of: "\n", with: "")
27+
28+
/// remove anything that is between a "don't()" and a "do()"
29+
/// be careful to match as few characters as possible with `.*?`
30+
let newData = singleLine.replacing(
31+
/(don't\(\).*?do\(\))/, with: "")
32+
33+
return compute(data: newData)
2434
}
2535
}

Sources/Extensions/String+Parsing.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ extension StringProtocol {
66
self.trimmingCharacters(in: .whitespacesAndNewlines)
77
}
88

9+
func concatLines() -> String {
10+
self.replacingOccurrences(of: "\n", with: "")
11+
}
12+
913
func lines() -> [SubSequence] {
1014
self.split(separator: "\n")
1115
}

Tests/Day03.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ struct Day03Tests {
1616

1717
@Test("part2")
1818
func testPart2() async throws {
19-
let challenge = Day03(data: testData)
20-
#expect(challenge.part2() == 0)
19+
let challenge = Day03(
20+
data: """
21+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)uno()?mul(8,5))
22+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
23+
""")
24+
#expect(challenge.part2() == 48)
25+
}
26+
27+
@Test("edges")
28+
func testEdges() async throws {
29+
let challenge = Day03(
30+
data: """
31+
don't()ado()_mul(5,5)do()mul(8,5)
32+
""")
33+
#expect(challenge.part2() == 25 + (8 * 5))
2134
}
2235
}

Tests/Extensions/String+Parsing.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ struct StringExtensions {
1010
#expect("\n\nhello\n".trimmed() == "hello")
1111
}
1212

13+
@Test("Concatenate Lines")
14+
func testConcatLines() async throws {
15+
#expect("h\ne\nl\nl\no".concatLines() == "hello")
16+
}
17+
1318
@Test("Lines Parsing")
1419
func testLinesParsing() async throws {
1520
#expect("".lines() == [])

0 commit comments

Comments
 (0)