generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.kt
240 lines (203 loc) · 6.33 KB
/
Day16.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package year2024.`16`
import readInput
import java.util.PriorityQueue
private const val CURRENT_DAY = "16"
private enum class Direction {
LEFT, RIGHT, DOWN, TOP;
}
private fun Direction.rotateClockWise(): Direction {
return when (this) {
Direction.LEFT -> Direction.TOP
Direction.RIGHT -> Direction.DOWN
Direction.DOWN -> Direction.LEFT
Direction.TOP -> Direction.RIGHT
}
}
private fun Direction.rotateCounterClockWise(): Direction {
return when (this) {
Direction.LEFT -> Direction.DOWN
Direction.RIGHT -> Direction.TOP
Direction.DOWN -> Direction.RIGHT
Direction.TOP -> Direction.LEFT
}
}
private data class Point(
val x: Int,
val y: Int,
) {
fun moveUp(): Point = copy(y = y - 1)
fun moveDown(): Point = copy(y = y + 1)
fun moveLeft(): Point = copy(x = x - 1)
fun moveRight(): Point = copy(x = x + 1)
fun moveWithDirection(direction: Direction): Point {
return when (direction) {
Direction.LEFT -> moveLeft()
Direction.RIGHT -> moveRight()
Direction.DOWN -> moveDown()
Direction.TOP -> moveUp()
}
}
fun moveWithOppositeDirection(direction: Direction): Point {
return when (direction) {
Direction.LEFT -> moveRight()
Direction.RIGHT -> moveLeft()
Direction.DOWN -> moveUp()
Direction.TOP -> moveDown()
}
}
override fun toString(): String {
return "[$x,$y]"
}
}
private sealed class BlockType {
abstract val position: Point
data class RainDeer(
override val position: Point,
) : BlockType() {
override fun toString(): String = "S"
}
data class End(
override val position: Point,
) : BlockType() {
override fun toString(): String = "E"
}
data class Wall(
override val position: Point,
) : BlockType() {
override fun toString(): String = "#"
}
data class Empty(
override val position: Point,
) : BlockType() {
override fun toString(): String = "."
}
}
private fun parseMap(input: List<String>): Map<Point, BlockType> {
val mutableMap = mutableMapOf<Point, BlockType>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
val point = Point(x, y)
val result = when (value) {
"#" -> BlockType.Wall(point)
"." -> BlockType.Empty(point)
"S" -> BlockType.RainDeer(point)
"E" -> BlockType.End(point)
else -> error("AAAA $value")
}
mutableMap[point] = result
}
}
return mutableMap
}
private data class StepWithScore(
val position: Point,
val score: Long,
val direction: Direction,
val prevPath: Set<Point> = setOf(position),
) {
fun toCacheWithValue(): CacheWithValue {
return CacheWithValue(
position,
direction,
)
}
}
private data class CacheWithValue(
val position: Point,
val direction: Direction,
)
private fun StepWithScore.calculateNextSteps(
initialMap: Map<Point, BlockType>,
): List<StepWithScore> {
return listOfNotNull(
StepWithScore(
this.position.moveWithDirection(direction),
score = score + 1,
direction = direction,
prevPath = prevPath + position.moveWithDirection(direction),
).takeIf { initialMap[it.position] !is BlockType.Wall },
StepWithScore(
position = position,
score = score + 1000,
direction = direction.rotateClockWise(),
prevPath = prevPath
),
StepWithScore(
position = position,
score = score + 1000,
direction = direction.rotateCounterClockWise(),
prevPath = prevPath
),
)
}
private fun dfsSearch(
initialMap: Map<Point, BlockType>,
currentRaindeerPosition: Point,
topPosition: Point,
): Pair<Long, Long> {
val queue = PriorityQueue<StepWithScore>(compareBy<StepWithScore> { it.score })
queue.add(
StepWithScore(
position = currentRaindeerPosition,
score = 0,
direction = Direction.RIGHT,
)
)
val cache = mutableMapOf<CacheWithValue, Long>()
var min = Long.MAX_VALUE
val best = mutableSetOf<Point>()
val endingSteps = mutableSetOf<StepWithScore>()
while (queue.isNotEmpty()) {
val currentStep = queue.poll()
val cacheWithValue = currentStep.toCacheWithValue()
if (initialMap[currentStep.position] is BlockType.End) {
if (currentStep.score <= min) {
min = currentStep.score
best.addAll(currentStep.prevPath)
}
endingSteps.add(currentStep)
continue
}
if (cacheWithValue in cache) {
if (cache[cacheWithValue]!! < currentStep.score) continue
}
cache[cacheWithValue] = currentStep.score
queue.addAll(
currentStep.calculateNextSteps(initialMap)
)
}
return endingSteps.minBy { it.score }.score to best.size.toLong()
}
fun main() {
fun part1(input: List<String>): Long {
val map = parseMap(input)
val position = map.keys.find { map[it] is BlockType.RainDeer }!!
val topPos = map.keys.find { map[it] is BlockType.End }!!
return dfsSearch(map, position, topPos).first
}
fun part2(input: List<String>): Long {
val map = parseMap(input)
val position = map.keys.find { map[it] is BlockType.RainDeer }!!
val topPos = map.keys.find { map[it] is BlockType.End }!!
return dfsSearch(map, position, topPos).second
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 7036L)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 45L)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 99448L)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 498L)
}