Skip to content

Commit 98f3690

Browse files
committed
day3
1 parent 87ef064 commit 98f3690

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Sources/Day03.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import Algorithms
22

33
struct Day03: AdventDay {
4-
// Save your data in a corresponding text file in the `Data` directory.
54
var data: String
65

7-
// add here any computed values useful for the challenge
8-
96
func part1() -> Int {
10-
0
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+
print(string)
12+
let numbers = string.split(
13+
separator: ","
14+
)
15+
print(numbers)
16+
return numbers.map {
17+
Int($0)
18+
}.compactMap {
19+
$0
20+
}.reduce(1, *)
21+
}.sum
1122
}
1223

1324
func part2() -> Int {

Tests/Day03.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import Testing
55
@Suite("Day03")
66
struct Day03Tests {
77
let testData = """
8-
8+
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
99
"""
1010

1111
@Test("part1")
1212
func testPart1() async throws {
1313
let challenge = Day03(data: testData)
14-
#expect(challenge.part1() == 0)
14+
#expect(challenge.part1() == 161)
1515
}
1616

1717
@Test("part2")

0 commit comments

Comments
 (0)