generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15.kt
131 lines (105 loc) · 3.92 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package year2021.`15`
import readInput
import java.util.*
private data class Point(
val x: Int,
val y: Int,
)
private typealias PointsToValues = Map<Point, Int>
private typealias MutablePointsToValues = MutableMap<Point, Int>
private fun infinitePaths(initialPoint: Point, initialValues: PointsToValues): PointsToValues {
return initialValues.mapValues {
if (it.key == initialPoint) {
0
} else {
Int.MAX_VALUE
}
}
}
private fun Point.neighbours(): Set<Point> {
return setOf(
Point(x = x, y = y + 1),
Point(x = x, y = y - 1),
Point(x = x + 1, y = y),
Point(x = x - 1, y = y),
)
}
private fun multiplyWithRulesBy25(initialPoints: PointsToValues, width: Int): PointsToValues {
val newPoints: MutablePointsToValues = mutableMapOf()
initialPoints.keys.forEach {
repeat(5) { i ->
repeat(5) { j ->
val sumIndex = i + j
val newX = width * i + it.x
val newY = width * j + it.y
val res = (initialPoints.getValue(it) + sumIndex)
val newRes = if (res > 9) {
res - 9
} else {
res
}
newPoints[Point(newX, newY)] = newRes
}
}
}
return newPoints
}
fun main() {
fun parse(input: List<String>): PointsToValues {
val mutableMap = mutableMapOf<Point, Int>()
input.forEachIndexed { y, line ->
line.forEachIndexed { x, number ->
mutableMap[Point(x, y)] = number.digitToInt()
}
}
return mutableMap
}
fun deikstra(initialPoint: Point, weights: PointsToValues): PointsToValues {
val currentMutableValuesHolder = infinitePaths(initialPoint, weights).toMutableMap()
val priorityQueue = PriorityQueue(compareBy<Point> { currentMutableValuesHolder[it] })
val visitedPoints = mutableSetOf<Point>()
priorityQueue.add(initialPoint)
while (priorityQueue.isNotEmpty()) {
val currentElement = priorityQueue.poll()
currentElement.neighbours()
.filter { it in currentMutableValuesHolder.keys }
.filter { it !in visitedPoints }
.forEach { nextPoint ->
val distanceToNextPoint = currentMutableValuesHolder.getValue(nextPoint)
val distanceToCurrentPoint = currentMutableValuesHolder.getValue(currentElement)
val weightOfNextPoint = weights.getValue(nextPoint)
if (distanceToNextPoint > distanceToCurrentPoint + weightOfNextPoint) {
currentMutableValuesHolder[nextPoint] =
distanceToCurrentPoint + weightOfNextPoint
priorityQueue.add(nextPoint)
}
}
visitedPoints.add(currentElement)
}
return currentMutableValuesHolder
}
fun part1(input: List<String>): Int {
val parsedValues = parse(input)
val result = deikstra(Point(0, 0), parsedValues)
val maxPoint = parsedValues.keys.maxBy { it.x + it.y }
return result.getValue(maxPoint)
}
fun part2(input: List<String>): Int {
val parsedValues = parse(input)
val newRes = multiplyWithRulesBy25(parsedValues, parsedValues.keys.maxOf { it.x }.inc())
val result = deikstra(Point(0, 0), newRes)
val maxPoint = newRes.keys.maxBy { it.x + it.y }
return result.getValue(maxPoint)
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day15_test")
val part1Test = part1(testInput)
val part2Test = part2(testInput)
println(part1Test)
check(part1Test == 40)
println(part2Test)
check(part2Test == 315)
val input = readInput("Day15")
println(part1(input))
println(part2(input))
}