@@ -26,25 +26,32 @@ data class Board(val rows: List<String>) {
26
26
operator fun get (pos : Vec2 ): Char = rows[pos.y][pos.x]
27
27
28
28
fun shortestPath (start : Vec2 , end : Vec2 ): Int {
29
- val queue = PriorityQueue <Node >(11 ) { l, r -> r.total - l.total }
29
+ // Your run-of-the-mill Dijkstra implementation below
30
+
31
+ val queue = PriorityQueue <Node >(11 ) { l, r -> l.total - r.total }
30
32
val visited = mutableSetOf<Pair <Vec2 , Vec2 >>()
31
33
32
- val startDir: Vec2 = Vec2 (0 , - 1 )
34
+ val startDir: Vec2 = Vec2 (1 , 0 )
33
35
queue.add(Node (0 , start, startDir))
34
36
visited.add(Pair (start, startDir))
35
37
36
38
while (! queue.isEmpty()) {
37
39
val node = queue.poll()
38
- println (" ${node.pos} and ${this [node.pos]} " )
39
40
if (node.pos == end) {
40
41
return node.total
41
42
}
42
43
visited.add(Pair (node.pos, node.dir))
43
- for (dir in listOf (node.dir, node.dir.rotateCW(), node.dir.rotateCCW())) {
44
- val next = node.pos + dir
45
- val weight = if (dir == node.dir) 1 else 1001
46
- if (Pair (next, dir) !in visited && this [next] != ' #' ) {
47
- queue.add(Node (node.total + weight, next, dir))
44
+
45
+ // Step
46
+ val next = node.pos + node.dir
47
+ if (Pair (next, node.dir) !in visited && this [next] != ' #' ) {
48
+ queue.add(Node (node.total + 1 , next, node.dir))
49
+ }
50
+
51
+ // Rotate
52
+ for (dir in listOf (node.dir.rotateCW(), node.dir.rotateCCW())) {
53
+ if (Pair (node.pos, dir) !in visited) {
54
+ queue.add(Node (node.total + 1000 , node.pos, dir))
48
55
}
49
56
}
50
57
}
@@ -59,4 +66,4 @@ val board = Board(File(args[0]).readLines())
59
66
val start = Vec2 (1 , board.height - 2 )
60
67
val end = Vec2 (board.width - 2 , 1 )
61
68
62
- print (" Part 1: ${board.shortestPath(start, end)} " )
69
+ println (" Part 1: ${board.shortestPath(start, end)} " )
0 commit comments