-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path21.swift
304 lines (258 loc) · 7.36 KB
/
21.swift
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// The Garden of Forking Paths
typealias Map = [[Character]]
struct Coordinate: Hashable {
let x, y: Int
}
struct Grid {
var map: Map
let start: Coordinate
let max: Coordinate
init(map: Map) {
self.map = map
self.start = Grid.findStart(map)
self.max = Coordinate(x: map[0].count - 1, y: map.count - 1)
}
static func findStart(_ map: Map) -> Coordinate {
for (y, row) in map.enumerated() {
for (x, c) in row.enumerated() {
if c == "S" { return Coordinate(x: x, y: y) }
}
}
fatalError()
}
func at(_ c: Coordinate) -> Character {
map[c.y][c.x]
}
func neighbours(of c: Coordinate) -> [Coordinate] {
potentialNeighbours(c).filter { inBounds($0) && at($0) != "#" }
}
func inBounds(_ c: Coordinate) -> Bool {
c.x >= 0 && c.x <= max.x &&
c.y >= 0 && c.y <= max.y
}
func potentialNeighbours(_ c: Coordinate) -> [Coordinate] {
[(c.x - 1, c.y), (c.x + 1, c.y), (c.x, c.y - 1), (c.x, c.y + 1)]
.map { Coordinate(x: $0, y: $1) }
}
func coordinates() -> [Coordinate] {
var result: [Coordinate] = []
for y in 0...max.y {
for x in 0...max.x {
result.append(Coordinate(x: x, y: y))
}
}
return result
}
mutating func move() {
func isOccupied(_ c: Character) -> Bool {
c == "O" || c == "S"
}
let prevMap = map
let cs = coordinates()
for c in cs {
if isOccupied(map[c.y][c.x]) {
map[c.y][c.x] = "."
}
}
for c in cs {
if isOccupied(prevMap[c.y][c.x]) {
for n in neighbours(of: c) {
map[n.y][n.x] = "O"
}
}
}
}
var reachable: Int {
map.reduce(0, { $1.reduce($0, { $0 + ($1 == "O" ? 1 : 0) })})
}
}
extension Grid: CustomStringConvertible {
var description: String {
map.map { String($0) }.joined(separator: "\n")
}
}
// MARK: P2 ---
// Each cell here tracks if it is occupied in the superposition of all the
// possible grids that are overlaid on top of each other. This is easy to do for
// this particular problem - we can just wrap around at the bounds. This works
// because the next step does not depend on the neighbours (like in the game of
// life), but instead we can just propogate our current count of occupancy to
// our neighbours.
struct QuantumGrid {
var map: [[Int]]
let start: Coordinate
let max: Coordinate
init(map: Map) {
self.map = Self.makeCountMap(map)
self.start = Self.findStart(map)
self.max = Coordinate(x: map[0].count - 1, y: map.count - 1)
}
static func makeCountMap(_ map: Map) -> [[Int]] {
var result = [[Int]]()
for row in map {
result.append(row.map { cell in switch cell {
case "#": -1
case "S": 1
default: 0
}})
}
return result
}
static func findStart(_ map: Map) -> Coordinate {
for (y, row) in map.enumerated() {
for (x, c) in row.enumerated() {
if c == "S" { return Coordinate(x: x, y: y) }
}
}
fatalError()
}
func at(_ c: Coordinate) -> Int {
map[c.y][c.x]
}
func neighbours(of c: Coordinate) -> [Coordinate] {
potentialNeighbours(c).filter { at($0) >= 0 }
}
func wrapAtBounds(_ c: Coordinate) -> Coordinate {
Coordinate(x: c.x < 0 ? ((max.x + 1) + c.x) : (c.x % (max.x + 1)),
y: c.y < 0 ? ((max.y + 1) + c.y) : (c.y % (max.y + 1)))
}
func potentialNeighbours(_ c: Coordinate) -> [Coordinate] {
[(c.x - 1, c.y), (c.x + 1, c.y), (c.x, c.y - 1), (c.x, c.y + 1)]
.map { Coordinate(x: $0, y: $1) }
.map { wrapAtBounds($0) }
}
func coordinates() -> [Coordinate] {
var result: [Coordinate] = []
for y in 0...max.y {
for x in 0...max.x {
result.append(Coordinate(x: x, y: y))
}
}
return result
}
mutating func move() {
func isOccupied(_ i: Int) -> Bool {
i > 0
}
let prevMap = map
let cs = coordinates()
for c in cs {
if isOccupied(map[c.y][c.x]) {
map[c.y][c.x] = 0
}
}
for c in cs {
let p = prevMap[c.y][c.x]
if p > 0 {
for n in neighbours(of: c) {
print("map[\(n.y)][\(n.x)] += \(p)")
map[n.y][n.x] += p
}
}
}
}
var reachable: Int {
map.reduce(0, { $1.reduce($0, { $0 + ($1 > 0 ? 1 : 0) })})
}
}
extension QuantumGrid: CustomStringConvertible {
var description: String {
func pad5(_ s: String) -> String {
if s.count > 5 { ">9999" } else { String((" " + s).suffix(5)) }
}
func mapCell(_ i: Int) -> String {
pad5(String(i < 0 ? "#" : (i == 0 ? "." : "\(i)")))
}
func mapRow(_ xs: [Int]) -> String {
xs.map({ mapCell($0) }).joined(separator: "")
}
return map.map { mapRow($0) }.joined(separator: "\n")
}
}
// MARK: Normal programming resumes
// For running under LLDB, where reading from stdin is a pain.
//
// However, we the input fixed, we can do this:
//
// swiftc -O -o out/21.sw 21.swift
// lldb out/21.sw
// > run
let example = """
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
"""
func _readInput() -> (Grid, QuantumGrid) {
var map: Map = []
while let line = readLine() {
map.append(Array(line))
}
return (Grid(map: map), QuantumGrid(map: map))
}
func readInput() -> (Grid, QuantumGrid) {
var map: Map = []
for line in example.split(separator: "\n") {
map.append(Array(line))
}
return (Grid(map: map), QuantumGrid(map: map))
}
func bfs(grid: Grid, maxStep: Int) -> Int {
var pending = [(grid.start, 0)]
var pi = 0
var lastCells: Set<Coordinate> = Set()
while pi < pending.count {
let (c, step) = pending[pi]
pi += 1
if step == maxStep {
lastCells.insert(c)
continue
}
for n in grid.neighbours(of: c) {
pending.append((n, step + 1))
}
}
return lastCells.count
}
func move(grid: inout Grid, steps: Int) {
for s in 1...steps {
grid.move()
if verbose > 0 {
print("after step \(s) we reached \(grid.reachable) cells")
print(grid)
print("")
}
}
}
func quantumMove(grid: inout QuantumGrid, steps: Int) {
for s in 1...steps {
grid.move()
if verbose > 0 {
print("after step \(s) we reached \(grid.reachable) cells")
print(grid)
print("")
}
}
}
let verbose = switch CommandLine.arguments.last {
case "-v": 1
case "-vv": 2
default: 0
}
var (grid, quantumGrid) = readInput()
// move(grid: &grid, steps: 6)
// let p1 = grid.reachable
// print(p1)
quantumMove(grid: &quantumGrid, steps: 6)
let p2 = quantumGrid.reachable
print(quantumGrid)
print(p2)
// let c = bfs(grid: grid, maxStep: 16)
// print(c)