File tree 1 file changed +28
-3
lines changed
1 file changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ struct Grid {
16
16
init ( map: Map ) {
17
17
self . map = map
18
18
self . start = Grid . findStart ( map)
19
- self . max = Coordinate ( x: map [ 0 ] . count, y: map. count)
19
+ self . max = Coordinate ( x: map [ 0 ] . count - 1 , y: map. count - 1 )
20
20
}
21
21
22
22
static func findStart( _ map: Map ) -> Coordinate {
@@ -61,10 +61,35 @@ func readInput() -> Grid {
61
61
return Grid ( map: map)
62
62
}
63
63
64
- func bfs( _ map : Map ) -> Int {
64
+ func bfs( grid : Grid , maxStep : Int ) -> Int {
65
65
var visited : Set < Coordinate > = Set ( )
66
- return 0
66
+
67
+ var pending = [ ( grid. start, 0 ) ]
68
+ var pi = 0
69
+
70
+ while pi < pending. count {
71
+ let ( c, step) = pending [ pi]
72
+ pi += 1
73
+
74
+ if !visited. insert ( c) . inserted {
75
+ continue
76
+ }
77
+
78
+ if step == maxStep {
79
+ continue
80
+ }
81
+
82
+ for n in grid. neighbours ( of: c) {
83
+ if !visited. contains ( n) {
84
+ pending. append ( ( n, step + 1 ) )
85
+ }
86
+ }
87
+ }
88
+
89
+ return visited. count
67
90
}
68
91
69
92
let grid = readInput ( )
93
+ let c = bfs ( grid: grid, maxStep: 64 )
70
94
print ( grid)
95
+ print ( c)
You can’t perform that action at this time.
0 commit comments