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
345 lines (285 loc) · 9.67 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package year2024.`15`
import readInput
import utils.printlnDebug
import utils.printDebug
private const val CURRENT_DAY = "15"
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()
}
}
override fun toString(): String {
return "[$x,$y]"
}
}
private enum class Direction { LEFT, RIGHT, DOWN, TOP }
private sealed class BlockType {
abstract val position: Point
data class Robot(
override val position: Point,
) : BlockType() {
override fun toString(): String = "@"
}
data class Box(
override val position: Point,
) : BlockType() {
override fun toString(): String = "O"
}
sealed class Box2 : BlockType() {
data class Left(
override val position: Point,
) : Box2() {
override fun toString(): String = "["
}
data class Right(
override val position: Point,
) : Box2() {
override fun toString(): String = "]"
}
}
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 prepareMapV2(input: List<String>): List<String> {
return input.map { line ->
line.toCharArray().joinToString("", "", "") { char ->
when (char) {
'#' -> "##"
'O' -> "[]"
'.' -> ".."
'@' -> "@."
else -> error("AAAA $char")
}
}
}
}
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)
"O" -> BlockType.Box(point)
"." -> BlockType.Empty(point)
"@" -> BlockType.Robot(point)
"[" -> BlockType.Box2.Left(point)
"]" -> BlockType.Box2.Right(point)
else -> error("AAAA $value")
}
mutableMap[point] = result
}
}
return mutableMap
}
private fun printMap(initialMap: Map<Point, BlockType>) {
val number = initialMap.maxOf { it.key.x + 1 }
val sortedPoints = initialMap.keys.toList()
.sortedWith(compareBy({ it.y }, { it.x }))
sortedPoints.forEachIndexed { index, point ->
if (index % number == 0) {
printlnDebug { }
}
printDebug { "" + initialMap[point] }
}
printlnDebug { }
}
private fun parseLineInto(
line: String,
): List<Direction> {
//<^^>>>vv<v>>v<<
return line.map {
when (it) {
'<' -> Direction.LEFT
'^' -> Direction.TOP
'>' -> Direction.RIGHT
'v' -> Direction.DOWN
else -> error("")
}
}
}
private fun performMovement(
input: Map<Point, BlockType>,
movement: Direction,
shouldPrintDebug: Boolean,
): Map<Point, BlockType> {
val robotPosition =
input.keys.find { input[it] is BlockType.Robot } ?: error("impossible state robot should be present")
val (mapOfLine, canMove) = findAllLineOfItems(input, movement, robotPosition, shouldPrintDebug)
val resultMap = input.toMutableMap()
if (canMove) {
mapOfLine.forEach {
resultMap[it] = BlockType.Empty(it)
}
mapOfLine.forEach {
val itemToMove = input[it]!!
val newPosition = it.moveWithDirection(direction = movement)
resultMap[newPosition] = itemToMove
if (itemToMove is BlockType.Robot) {
resultMap[it] = BlockType.Empty(it)
}
}
}
return resultMap
}
/**
* true - can be shifted
*/
private fun findAllLineOfItems(
input: Map<Point, BlockType>,
movement: Direction,
initPosition: Point,
shouldPrintDebug: Boolean,
): Pair<List<Point>, Boolean> {
val resultList = mutableListOf<Point>()
var currentPosition = initPosition
while (input[currentPosition] != null &&
input[currentPosition] !is BlockType.Wall
&& input[currentPosition] !is BlockType.Empty
) {
resultList.add(currentPosition)
val nextPosition = when (movement) {
Direction.LEFT -> currentPosition.moveLeft()
Direction.RIGHT -> currentPosition.moveRight()
Direction.DOWN -> {
val downPos = currentPosition.moveDown()
val item = input[downPos]
when (item) {
is BlockType.Box2.Left -> {
val (checkBottomList, checkBottomCan) = findAllLineOfItems(
input,
movement,
downPos.moveRight(),
shouldPrintDebug
)
resultList.addAll(checkBottomList)
}
is BlockType.Box2.Right -> {
val (checkBottomList, checkBottomCan) = findAllLineOfItems(
input,
movement,
downPos.moveLeft(),
shouldPrintDebug
)
resultList.addAll(checkBottomList)
}
else -> {}
}
downPos
}
Direction.TOP -> {
val topPos = currentPosition.moveUp()
val item = input[topPos]
when (item) {
is BlockType.Box2.Left -> {
val (checkTopList, checkTopCan) = findAllLineOfItems(
input,
movement,
topPos.moveRight(),
shouldPrintDebug,
)
resultList.addAll(checkTopList)
}
is BlockType.Box2.Right -> {
val (checkTopList, checkTopCan) = findAllLineOfItems(
input,
movement,
topPos.moveLeft(),
shouldPrintDebug,
)
resultList.addAll(checkTopList)
}
else -> {}
}
topPos
}
}
currentPosition = nextPosition
}
val canMove = input[currentPosition] is BlockType.Empty
val resultCanMove = resultList.none { input[it.moveWithDirection(movement)] is BlockType.Wall }
if (shouldPrintDebug) {
val currentPrintItems = resultList.map { input[it] }
val nextPrintItems = resultList.map { input[it.moveWithDirection(movement)] }
println("LIST: $resultList CAN MOVE :$resultCanMove \nItems: ${currentPrintItems} \nNextItems:$nextPrintItems")
println()
}
return resultList to resultCanMove
}
private fun calculateGpsResult(input: Map<Point, BlockType>): Int {
var result = 0
input.keys.forEach {
val currentItem = input[it]!!
if (currentItem is BlockType.Box || currentItem is BlockType.Box2.Left) {
result += 100 * it.y + it.x
}
}
return result
}
fun main() {
fun part1(input: List<String>): Int {
val blocks = input.takeWhile { it.isNotBlank() }
val blocksMap = parseMap(blocks)
val movementsLine = input.reversed().takeWhile { it.isNotBlank() }.reversed().joinToString("", "", "")
val movements = parseLineInto(movementsLine)
var currentStateMap = blocksMap
movements.forEach {
currentStateMap = performMovement(currentStateMap, it, false)
}
return calculateGpsResult(currentStateMap)
}
fun part2(input: List<String>): Int {
val blocks = input.takeWhile { it.isNotBlank() }
val blocksMap = parseMap(prepareMapV2(blocks))
printMap(blocksMap)
val movementsLine = input.reversed().takeWhile { it.isNotBlank() }.reversed().joinToString("", "", "")
val movements = parseLineInto(movementsLine)
var currentStateMap = blocksMap
movements.forEachIndexed { index, direction ->
currentStateMap = performMovement(currentStateMap, direction, false)
}
println()
println("LAST")
printMap(currentStateMap)
//1445099
return calculateGpsResult(currentStateMap)
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 10092)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 9021)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 1478649)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 1495455)
}