3
3
// Part 1: Do a BFS to find all reachable nodes within 64 steps.
4
4
5
5
typealias Map = [ [ Character ] ]
6
- typealias Coordinate = ( x: Int , y: Int )
6
+
7
+ struct Coordinate : Hashable {
8
+ let x , y : Int
9
+ }
7
10
8
11
struct Grid {
9
12
let map : Map
@@ -13,20 +16,34 @@ struct Grid {
13
16
init ( map: Map ) {
14
17
self . map = map
15
18
self . start = Grid . findStart ( map)
16
- self . max = ( map [ 0 ] . count, map. count)
19
+ self . max = Coordinate ( x : map [ 0 ] . count, y : map. count)
17
20
}
18
21
19
22
static func findStart( _ map: Map ) -> Coordinate {
20
23
for (y, row) in map. enumerated ( ) {
21
24
for (x, c) in row. enumerated ( ) {
22
- if c == " S " { return ( x , y) }
25
+ if c == " S " { return Coordinate ( x : x , y : y) }
23
26
}
24
27
}
25
28
fatalError ( )
26
29
}
27
30
31
+ func at( _ c: Coordinate ) -> Character {
32
+ map [ c. y] [ c. x]
33
+ }
34
+
28
35
func neighbours( of c: Coordinate ) -> [ Coordinate ] {
29
- return [ ]
36
+ potentialNeighbours ( c) . filter { inBounds ( $0) && at ( $0) != " # " }
37
+ }
38
+
39
+ func inBounds( _ c: Coordinate ) -> Bool {
40
+ c. x >= 0 && c. x <= max. x &&
41
+ c. y >= 0 && c. y <= max. y
42
+ }
43
+
44
+ func potentialNeighbours( _ c: Coordinate ) -> [ Coordinate ] {
45
+ [ ( c. x - 1 , c. y) , ( c. x + 1 , c. y) , ( c. x, c. y - 1 ) , ( c. x, c. y + 1 ) ]
46
+ . map { Coordinate ( x: $0, y: $1) }
30
47
}
31
48
}
32
49
@@ -44,9 +61,10 @@ func readInput() -> Grid {
44
61
return Grid ( map: map)
45
62
}
46
63
47
- // func bfs(_ map: Map) -> Int {
48
- // var visited: Set
49
- // }
64
+ func bfs( _ map: Map ) -> Int {
65
+ var visited : Set < Coordinate > = Set ( )
66
+ return 0
67
+ }
50
68
51
69
let grid = readInput ( )
52
70
print ( grid)
0 commit comments