Skip to content

Commit a810c7a

Browse files
committed
p1 attempt
1 parent 023ba87 commit a810c7a

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

17.wip.swift

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,34 @@ func readInput() -> Grid {
2121
return Grid(items: items, lastNode: .init(x: x - 1, y: y - 1))
2222
}
2323

24-
func shortestPath(grid: Grid) -> Int? {
24+
enum Direction {
25+
case l, r, u, d
26+
}
27+
28+
struct Cell: Hashable {
29+
let node: Node
30+
let direction: Direction
31+
let moves: Int
32+
}
33+
34+
func shortestPath(grid: Grid, moveRange: ClosedRange<Int>) -> Int? {
2535
let startNode = Node(x: 0, y: 0)
26-
func isEnd(node: Node) -> Bool { node == grid.lastNode }
36+
// Setting moves to 0 to allows us to equally consider both left and down
37+
// neighbours.
38+
let startCell = Cell(node: startNode, direction: .l, moves: 0)
39+
func isEnd(_ cell: Cell) -> Bool { cell.node == grid.lastNode }
2740

28-
var dist: [Node: Int] = [startNode: 0]
29-
var seen: Set<Node> = Set()
41+
func adj(_ u: Cell) -> [Cell] {
42+
neighbours(grid: grid, moveRange: moveRange, cell: u)
43+
}
44+
45+
var dist: [Cell: Int] = [startCell: 0]
46+
var seen: Set<Cell> = Set()
3047
//var invDist: [Int: [Node]] = []
3148
//var minDist = 0
3249

33-
func popNearest() -> (Node, Int)? {
34-
var u: Node?
50+
func popNearest() -> (Cell, Int)? {
51+
var u: Cell?
3552
var du: Int = .max
3653
for (v, dv) in dist {
3754
if dv < du && !seen.contains(v) {
@@ -45,9 +62,9 @@ func shortestPath(grid: Grid) -> Int? {
4562

4663
while let (u, du) = popNearest() {
4764
if !seen.insert(u).inserted { continue }
48-
if isEnd(node: u) { return du }
49-
for v in neighbours(grid: grid, node: u) {
50-
let d = grid.items[v]!
65+
if isEnd(u) { return du }
66+
for v in adj(u) {
67+
let d = grid.items[v.node]!
5168
let dv = dist[v] ?? .max
5269
if du + d < dv {
5370
dist[v] = du + d
@@ -58,13 +75,39 @@ func shortestPath(grid: Grid) -> Int? {
5875
return nil
5976
}
6077

61-
func neighbours(grid: Grid, node: Node) -> [Node] {
78+
func neighbours(grid: Grid, moveRange: ClosedRange<Int>, cell: Cell) -> [Cell] {
79+
func make(_ xy: (Int, Int), _ direction: Direction, _ moves: Int) -> Cell {
80+
Cell(node: Node(x: xy.0, y: xy.1), direction: direction, moves: moves)
81+
}
82+
83+
let node = cell.node
84+
let moves = cell.moves
6285
let (x, y) = (node.x, node.y)
63-
return [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
64-
.map { Node(x: $0.0, y: $0.1) }
65-
.filter { grid.items[$0] != nil }
86+
let cells: [Cell]
87+
switch cell.direction {
88+
case .l:
89+
cells = [make((x + 1, y), .l, moves + 1),
90+
make((x, y - 1), .u, 1),
91+
make((x, y + 1), .d, 1)]
92+
case .r:
93+
cells = [make((x - 1, y), .r, moves + 1),
94+
make((x, y - 1), .u, 1),
95+
make((x, y + 1), .d, 1)]
96+
case .u:
97+
cells = [make((x, y - 1), .u, moves + 1),
98+
make((x - 1, y), .r, 1),
99+
make((x + 1, y), .l, 1)]
100+
case .d:
101+
cells = [make((x, y + 1), .d, moves + 1),
102+
make((x - 1, y), .r, 1),
103+
make((x + 1, y), .l, 1)]
104+
}
105+
return cells
106+
.filter { grid.items[$0.node] != nil }
107+
.filter { moveRange.contains($0.moves) }
66108
}
67109

68110
let grid = readInput()
69-
let sp = shortestPath(grid: grid)
70-
print(sp ?? 0)
111+
let p1 = shortestPath(grid: grid, moveRange: 1...3)
112+
// let p1 = shortestPath(grid: grid, moveRange: 4...10)
113+
print(p1 ?? 0)

0 commit comments

Comments
 (0)