Skip to content

Commit ab8555f

Browse files
committed
Put it in a viewable struct
1 parent 47055a6 commit ab8555f

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

21.swift

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,51 @@
22
//
33
// Part 1: Do a BFS to find all reachable nodes within 64 steps.
44

5-
func readInput() -> [[Character]] {
6-
var result: [[Character]] = []
5+
typealias Map = [[Character]]
6+
typealias Coordinate = (x: Int, y: Int)
7+
8+
struct Grid {
9+
let map: Map
10+
let start: Coordinate
11+
let max: Coordinate
12+
13+
init(map: Map) {
14+
self.map = map
15+
self.start = Grid.findStart(map)
16+
self.max = (map[0].count, map.count)
17+
}
18+
19+
static func findStart(_ map: Map) -> Coordinate {
20+
for (y, row) in map.enumerated() {
21+
for (x, c) in row.enumerated() {
22+
if c == "S" { return (x, y) }
23+
}
24+
}
25+
fatalError()
26+
}
27+
28+
func neighbours(of c: Coordinate) -> [Coordinate] {
29+
return []
30+
}
31+
}
32+
33+
extension Grid: CustomStringConvertible {
34+
var description: String {
35+
map.map { String($0) }.joined(separator: "\n")
36+
}
37+
}
38+
39+
func readInput() -> Grid {
40+
var map: Map = []
741
while let line = readLine() {
8-
result.append(Array(line))
42+
map.append(Array(line))
943
}
10-
return result
44+
return Grid(map: map)
1145
}
1246

13-
let map = readInput()
14-
print(map)
47+
// func bfs(_ map: Map) -> Int {
48+
// var visited: Set
49+
// }
50+
51+
let grid = readInput()
52+
print(grid)

0 commit comments

Comments
 (0)