Skip to content

Commit 2becdbb

Browse files
committed
day10 woohooo
1 parent c13fa1c commit 2becdbb

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Below is my current advancement in Advent of code:
1717
| Day 7 | :star: (45min) | :star: (33min) |
1818
| Day 8 | :star: (20min) | :star: (16min) |
1919
| Day 9 | :star: (1h51) | :star: (43min) |
20-
| Day 10 | | |
20+
| Day 10 | :star: (28min) | :star: (3min) |
2121
| Day 11 | | |
2222
| Day 12 | | |
2323
| Day 13 | | |

Sources/AdventOfCode.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let allChallenges: [any AdventDay] = [
1111
Day07(),
1212
Day08(),
1313
Day09(),
14+
Day10(),
1415
]
1516

1617
@main

Sources/Day10.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Algorithms
2+
3+
struct Day10: AdventDay {
4+
var grid: Grid
5+
6+
init(data: String) {
7+
grid = Grid(from: data)
8+
}
9+
10+
func findNextStep(for startChar: String, at: Coord, summits: [Coord]) -> [Coord] {
11+
var results = summits
12+
if startChar == "9" {
13+
results.append(at)
14+
return results
15+
}
16+
let nextChar = String((Int(startChar) ?? 0) + 1)
17+
for neighbor in at.crossNeighbors {
18+
if let candidate = grid[neighbor], String(candidate) == nextChar {
19+
let res = findNextStep(for: nextChar, at: neighbor, summits: summits)
20+
results.append(contentsOf: res)
21+
} else {
22+
}
23+
}
24+
return results
25+
}
26+
27+
func part1() -> Int {
28+
grid.findAll(of: "0").map {
29+
Set(findNextStep(for: "0", at: $0, summits: [Coord]())).count
30+
}.sum
31+
}
32+
33+
func part2() -> Int {
34+
grid.findAll(of: "0").map {
35+
findNextStep(for: "0", at: $0, summits: [Coord]()).count
36+
}.sum
37+
}
38+
}

Sources/Helpers/Grid.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ struct Grid {
1111
raw.count
1212
}
1313

14-
subscript(_ coord: Coord) -> Element {
15-
raw[coord.y][coord.x]
14+
subscript(_ coord: Coord) -> Element? {
15+
if coord.isInside(grid: self) {
16+
return raw[coord.y][coord.x]
17+
}
18+
return nil
1619
}
1720

1821
subscript(col: Int, row: Int) -> Element {
@@ -46,4 +49,17 @@ extension Grid {
4649
}
4750
return nil
4851
}
52+
53+
func findAll(of char: Character) -> [Coord] {
54+
var coords = [Coord]()
55+
for i in 0..<height {
56+
for j in 0..<width {
57+
let current = Coord(x: j, y: i)
58+
if self[current] == char {
59+
coords.append(current)
60+
}
61+
}
62+
}
63+
return coords
64+
}
4965
}

Tests/Day10.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Testing
2+
3+
@testable import aoc
4+
5+
@Suite("Day10")
6+
struct Day10Tests {
7+
let testData = """
8+
89010123
9+
78121874
10+
87430965
11+
96549874
12+
45678903
13+
32019012
14+
01329801
15+
10456732
16+
"""
17+
18+
@Test("part1")
19+
func testPart1() async throws {
20+
let challenge = Day10(data: testData)
21+
#expect(challenge.part1() == 36)
22+
}
23+
24+
@Test("simple")
25+
func testSimple() async throws {
26+
#expect(
27+
Day10(
28+
data: """
29+
0123
30+
1234
31+
8765
32+
9876
33+
"""
34+
).part1() == 1)
35+
#expect(
36+
Day10(
37+
data: """
38+
9990999
39+
9991999
40+
9992999
41+
6543456
42+
7999997
43+
8599958
44+
9999999
45+
"""
46+
).part1() == 2)
47+
}
48+
49+
@Test("part2")
50+
func testPart2() async throws {
51+
let challenge = Day10(data: testData)
52+
#expect(challenge.part2() == 81)
53+
}
54+
}

0 commit comments

Comments
 (0)