-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.swift
More file actions
203 lines (169 loc) · 4.64 KB
/
Day10.swift
File metadata and controls
203 lines (169 loc) · 4.64 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import Algorithms
import AoCCommon
import Foundation
import GameplayKit
struct Day10: AdventDay, Sendable {
let data: String
let day = 10
let puzzleName: String = "--- Day 10: Hoof It ---"
let rows: [[Int]]
init(data: String) {
self.data = data
do {
rows = try SingleDigitLinesParser().parse(data)
} catch {
fatalError("Unable to parse input \(error)")
}
}
var grid: Grid<Int> {
do {
return try SingleDigitGridParser().parse(data)
} catch {
fatalError("Unable to parse data because \(error)")
}
}
func part1() async throws -> Int {
score(gridGraph)
// trailHeads(grid)
// .map { score(grid, start: $0) }
// .reduce(0, +)
}
func part2() async throws -> Int {
rating(gridGraph)
// trailHeads(grid)
// .map { rating(grid, start: $0) }
// .reduce(0, +)
}
}
// MARK: - Using Foundation
extension Day10 {
func trailHeads(_ grid: Grid<Int>) -> Set<Cell> {
grid.filter { $0 == 0 }
}
func trailCount(_ grid: Grid<Int>, start: Cell, allPaths: Bool = false) -> Int {
var count = 0
var queue = Deque<Cell>([start])
var ends = Set<Cell>()
while !queue.isEmpty {
let cursor = queue.removeFirst()
let cursorValue = grid.element(cursor)!
if cursorValue == 9 {
switch (allPaths, ends.contains(cursor)) {
case (false, false):
count += 1
ends.insert(cursor)
case (false, true):
continue
case (true, _):
count += 1
continue
}
}
let neighbours = grid
.neighbours(cursor, includeDiagonals: false)
.filter { grid.element($0)! - cursorValue == 1 }
queue.append(contentsOf: neighbours)
}
return count
}
func score(_ grid: Grid<Int>, start: Cell) -> Int {
trailCount(grid, start: start)
}
func rating(_ grid: Grid<Int>, start: Cell) -> Int {
trailCount(grid, start: start, allPaths: true)
}
}
// MARK: - Using GamePlayKit
extension Day10 {
typealias GridGraph = GKGridGraph<GKGridGraphNode>
typealias Node = GKGridGraphNode
var gridGraph: GridGraph {
let width = Int32(rows[0].count)
let height = Int32(rows.count)
let origin = vector_int2(0, 0)
let graph = GKGridGraph(
fromGridStartingAt: origin,
width: width,
height: height,
diagonalsAllowed: false,
nodeClass: Node.self
)
for node in graph.nodes! {
let node = node as! Node
let position = node.gridPosition
let (row, column) = (Int(position.y), Int(position.x))
for neighbour in node.connectedNodes {
let neighbour = neighbour as! Node
let nPosition = neighbour.gridPosition
let (nRow, nColumn) = (Int(nPosition.y), Int(nPosition.x))
if rows[nRow][nColumn] != rows[row][column] + 1 {
node.removeConnections(to: [neighbour], bidirectional: false)
}
}
}
return graph
}
func trailHeads(_ graph: GridGraph) -> [Node] {
nodes(for: 0, graph: graph)
}
func trailEnds(_ graph: GridGraph) -> [Node] {
nodes(for: 9, graph: graph)
}
func nodes(for value: Int, graph: GridGraph) -> [Node] {
graph
.nodes!
.compactMap { node -> Node? in
guard let node = node as? Node else { return nil }
return node
}
.filter { node in
let position = node.gridPosition
let (row, column) = (Int(position.y), Int(position.x))
return rows[row][column] == value
}
}
func countConnections(_: GridGraph, head: Node, ends: [Node]) -> Int {
var queue = Deque<Node>([head])
var seen = Set<Node>()
while !queue.isEmpty {
let node = queue.removeFirst()
if ends.contains(node) {
seen.insert(node)
continue
}
queue.append(contentsOf: node.connectedNodes as! [Node])
}
return seen.count
}
func countPaths(_: GridGraph, head: Node, ends: [Node]) -> Int {
var count = 0
var queue = Deque<Node>([head])
while !queue.isEmpty {
let node = queue.removeFirst()
if ends.contains(node) {
count += 1
continue
}
queue.append(contentsOf: node.connectedNodes as! [Node])
}
return count
}
func score(_ graph: GridGraph) -> Int {
let heads = trailHeads(graph)
let ends = trailEnds(graph)
var score = 0
for head in heads {
score += countConnections(graph, head: head, ends: ends)
}
return score
}
func rating(_ graph: GridGraph) -> Int {
let heads = trailHeads(graph)
let ends = trailEnds(graph)
var score = 0
for head in heads {
score += countPaths(graph, head: head, ends: ends)
}
return score
}
}