Skip to content

Commit 8ee28e0

Browse files
committed
Don't bring visited into consideration - works on the example
1 parent 8d80636 commit 8ee28e0

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

21.swift

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,29 @@ func readInput() -> Grid {
6262
}
6363

6464
func bfs(grid: Grid, maxStep: Int) -> Int {
65-
var visited: Set<Coordinate> = Set()
66-
6765
var pending = [(grid.start, 0)]
6866
var pi = 0
6967

68+
var lastCells: Set<Coordinate> = Set()
69+
7070
while pi < pending.count {
7171
let (c, step) = pending[pi]
7272
pi += 1
7373

74-
if !visited.insert(c).inserted {
75-
continue
76-
}
77-
7874
if step == maxStep {
75+
lastCells.insert(c)
7976
continue
8077
}
8178

8279
for n in grid.neighbours(of: c) {
83-
if !visited.contains(n) {
84-
pending.append((n, step + 1))
85-
}
80+
pending.append((n, step + 1))
8681
}
8782
}
8883

89-
return visited.count
84+
return lastCells.count
9085
}
9186

9287
let grid = readInput()
93-
let c = bfs(grid: grid, maxStep: 64)
94-
print(grid)
88+
let c = bfs(grid: grid, maxStep: 6)
89+
// print(grid)
9590
print(c)

0 commit comments

Comments
 (0)