-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.kt
57 lines (46 loc) · 1.71 KB
/
Day12.kt
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
import kotlin.math.min
private fun dijkstra(grid: List<List<Int>>, from: Point): List<List<Int>> {
val distances = MutableList(grid.size) { MutableList(grid.first().size) { Int.MAX_VALUE } }
val used = MutableList(grid.size) { MutableList(grid.first().size) { false } }
distances[from] = 0
for (iteration in 0 until grid.size * grid.first().size) {
val v = distances.innerIndexedSequence()
.filter { (i, j) -> !used[i][j] }
.minBy { it.value }
.let { (i, j) -> Point(i, j) }
if (distances[v] == Int.MAX_VALUE) break
used[v] = true
Direction.values()
.asSequence()
.map { it.point + v }
.filter { it.x in grid.indices && it.y in grid.first().indices }
.filter { grid[it] - grid[v] >= -1 }
.forEach { distances[it] = min(distances[it], distances[v] + 1) }
}
return distances
}
fun main() {
var from = Point(0, 0)
var to = Point(0, 0)
val grid = getFullInput()
.lineSequence()
.withIndex()
.map { (i, line) ->
line.asSequence()
.withIndex()
.map { (j, it) ->
when (it) {
in 'a'..'z' -> it
'S' -> 'a'.also { from = Point(i, j) }
'E' -> 'z'.also { to = Point(i, j) }
else -> expect()
} - 'a'
}
.toList()
}
.toList()
val distances = dijkstra(grid, to)
val first = distances[from]
val second = distances.innerIndexedSequence().filter { (i, j) -> grid[i][j] == 0 }.minOf { it.value }
println("$first $second")
}