-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day15.kt
41 lines (32 loc) · 1.36 KB
/
Day15.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
import kotlin.math.min
// asymptotic: O(V^2)
private fun dijkstra(matrix: List<List<Int>>): Int {
val distance = MutableList(matrix.size) { MutableList(matrix.first().size) { Int.MAX_VALUE } }
val used = MutableList(matrix.size) { MutableList(matrix.first().size) { false } }
distance[0][0] = 0
for (dummy in matrix.innerIndices) {
val vertex = matrix.innerIndices.filter { !used[it] }.minByOrNull { distance[it] }.expect()
if (distance[vertex] == Int.MAX_VALUE) break
used[vertex] = true
val (i, j) = vertex
sequenceOf(i - 1 to j, i + 1 to j, i to j - 1, i to j + 1)
.filter { (i, j) -> i in matrix.indices && j in matrix.first().indices }
.forEach { distance[it] = min(distance[it], distance[vertex] + matrix[it]) }
}
return distance.last().last()
}
private fun widen(matrix: List<List<Int>>) =
List(matrix.size * 5) { i ->
List(matrix.first().size * 5) { j ->
val increment = (i / matrix.size) + (j / matrix.first().size)
val value = matrix[i % matrix.size][j % matrix.first().size] + increment
if (value >= 10) value - 9 else value
}
}
fun main() {
val matrix = mapLines { line -> line.map { it.toString().toInt() } }
// runs for 3s
println(dijkstra(matrix))
// runs for 30min
println(dijkstra(widen(matrix)))
}