-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday16.kts
executable file
·80 lines (60 loc) · 2.26 KB
/
day16.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env kotlin
import java.io.File
import java.util.PriorityQueue
import kotlin.system.exitProcess
if (args.isEmpty()) {
println("Usage: day16 <path to input>")
exitProcess(1)
}
data class Vec2(val x: Int, val y: Int) {
fun rotateCW() = Vec2(-y, x)
fun rotateCCW() = Vec2(y, -x)
operator fun plus(rhs: Vec2) = Vec2(x + rhs.x, y + rhs.y)
}
data class Node(val total: Int, val visited: Set<Vec2>, val pos: Vec2, val dir: Vec2)
data class ShortestPaths(val length: Int, val visited: Set<Vec2>)
data class Board(val rows: List<String>) {
val height: Int get() = rows.size
val width: Int get() = rows[0].length
operator fun get(pos: Vec2): Char = rows[pos.y][pos.x]
fun shortestPaths(start: Vec2, end: Vec2): ShortestPaths {
// Your run-of-the-mill Dijkstra implementation below
val queue = PriorityQueue<Node>(11) { l, r -> l.total - r.total }
val visited = mutableSetOf<Pair<Vec2, Vec2>>()
val startDir: Vec2 = Vec2(1, 0)
queue.add(Node(0, setOf(start), start, startDir))
visited.add(Pair(start, startDir))
var shortestTotal: Int? = null
val shortestPathVisited = mutableSetOf<Vec2>()
while (!queue.isEmpty()) {
val node = queue.poll()
if (shortestTotal?.let { node.total > it } ?: false) {
break
}
if (node.pos == end) {
shortestTotal = node.total
shortestPathVisited.addAll(node.visited)
}
visited.add(Pair(node.pos, node.dir))
// Step
val next = node.pos + node.dir
if (Pair(next, node.dir) !in visited && this[next] != '#') {
queue.add(Node(node.total + 1, node.visited.union(listOf(next)), next, node.dir))
}
// Rotate
for (dir in listOf(node.dir.rotateCW(), node.dir.rotateCCW())) {
if (Pair(node.pos, dir) !in visited) {
queue.add(Node(node.total + 1000, node.visited, node.pos, dir))
}
}
}
return shortestTotal?.let { ShortestPaths(it, shortestPathVisited) } ?: throw RuntimeException("No path found")
}
}
val board = Board(File(args[0]).readLines())
// TODO: Don't hardcode these
val start = Vec2(1, board.height - 2)
val end = Vec2(board.width - 2, 1)
val result = board.shortestPaths(start, end)
println("Part 1: ${result.length}")
println("Part 2: ${result.visited.size}")