generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.kt
494 lines (438 loc) · 14.4 KB
/
Day17.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package year2022.`17`
import java.util.Collections
import readInput
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)
}
data class Figure(
val points: Set<Point>
) {
private val initialRange = 0..6
fun isOverlapping(pointSet: Set<Point>): Boolean = points.any { point ->
pointSet.contains(point)
}
fun moveLeft(
fallenPoints: Set<Point>,
range: IntRange = initialRange
): Figure {
val movedObj = copy(points = points.map { it.moveLeft() }.toSet())
return if (movedObj.points.any { it.x !in range } || movedObj.isOverlapping(fallenPoints)) {
this
} else {
movedObj
}
}
fun moveRight(
fallenPoints: Set<Point>,
range: IntRange = initialRange
): Figure {
val movedObj = copy(points = points.map { it.moveRight() }.toSet())
return if (movedObj.points.any { it.x !in range } || movedObj.isOverlapping(fallenPoints)) {
this
} else {
movedObj
}
}
fun moveDown(): Figure = copy(points = points.map { it.moveDown() }.toSet())
fun moveUp(): Figure = copy(points = points.map { it.moveUp() }.toSet())
companion object {
fun createFigure(
newFigureToCreate: FigureToCreate,
heightPoint: Point
) = when (newFigureToCreate) {
FigureToCreate.HORIZONTAL_LINE -> createLine(
heightPoint.copy(x = 2),
heightPoint.copy(y = heightPoint.y + 4)
)
FigureToCreate.CROSS -> createCross(
heightPoint.copy(x = 2),
heightPoint.copy(y = heightPoint.y + 4)
)
FigureToCreate.TRIANGLE -> createTriangle(
heightPoint.copy(x = 2),
heightPoint.copy(y = heightPoint.y + 4)
)
FigureToCreate.VERTICAL_LINE -> createVerticalLine(
heightPoint.copy(x = 2),
heightPoint.copy(y = heightPoint.y + 4)
)
FigureToCreate.SQUARE -> createSquare(
heightPoint.copy(x = 2),
heightPoint.copy(y = heightPoint.y + 4)
)
}
fun createFloor(): Figure {
return Figure(
setOf(
Point(0, 0),
Point(1, 0),
Point(2, 0),
Point(3, 0),
Point(4, 0),
Point(5, 0),
Point(6, 0),
)
)
}
private fun createLine(
initialLeftPoint: Point, // maybe remove
initialBottomPoint: Point
): Figure {
val startX = initialLeftPoint.x
val startY = initialBottomPoint.y
return Figure(
setOf(
Point(startX, startY),
Point(startX + 1, startY),
Point(startX + 2, startY),
Point(startX + 3, startY),
)
)
}
private fun createCross(
initialLeftPoint: Point, // maybe remove
initialBottomPoint: Point
): Figure {
val startX = initialLeftPoint.x
val startY = initialBottomPoint.y
return Figure(
setOf(
Point(startX + 1, startY),
Point(startX + 1, startY + 1),
Point(startX + 1, startY + 2),
Point(startX, startY + 1),
Point(startX + 2, startY + 1),
)
)
}
private fun createTriangle(
initialLeftPoint: Point, // maybe remove
initialBottomPoint: Point
): Figure {
val startX = initialLeftPoint.x
val startY = initialBottomPoint.y
return Figure(
setOf(
Point(startX, startY),
Point(startX + 1, startY),
Point(startX + 2, startY),
Point(startX + 2, startY + 1),
Point(startX + 2, startY + 2),
)
)
}
private fun createVerticalLine(
initialLeftPoint: Point, // maybe remove
initialBottomPoint: Point
): Figure {
val startX = initialLeftPoint.x
val startY = initialBottomPoint.y
return Figure(
setOf(
Point(startX, startY),
Point(startX, startY + 1),
Point(startX, startY + 2),
Point(startX, startY + 3),
)
)
}
private fun createSquare(
initialLeftPoint: Point, // maybe remove
initialBottomPoint: Point
): Figure {
val startX = initialLeftPoint.x
val startY = initialBottomPoint.y
return Figure(
setOf(
Point(startX, startY),
Point(startX, startY + 1),
Point(startX + 1, startY),
Point(startX + 1, startY + 1),
)
)
}
}
}
enum class FigureToCreate {
HORIZONTAL_LINE,
CROSS,
TRIANGLE,
VERTICAL_LINE,
SQUARE
}
enum class Movement {
LEFT, RIGHT
}
data class CycleInfoModel(
val figureWithNoOffset: Figure,
val currentFallingFigure: FigureToCreate,
val currentMovement: Movement,
val currentMovementIndex: Int
) {
companion object {
fun findCycleHelper(
points: Set<Point>,
currentFallingFigure: FigureToCreate,
prevMovement: Movement,
index: Int
): CycleInfoModel {
val topPointsSlice = points.sliceTopPoints()
val lowPoint = topPointsSlice.minBy { it.y }
val res = topPointsSlice
.map { it.copy(x = it.x, y = it.y - lowPoint.y) }
.toSet()
return CycleInfoModel(
figureWithNoOffset = Figure(res),
currentFallingFigure = currentFallingFigure,
currentMovement = prevMovement,
currentMovementIndex = index
)
}
}
}
fun parseCommands(input: List<String>): List<Movement> {
return input.map { it.split("") }
.flatten()
.filter { it.isNotBlank() }
.map {
when (it) {
">" -> Movement.RIGHT
"<" -> Movement.LEFT
else -> error("!!! $it")
}
}
}
/**
* TODO Write Custom Implementation instead of function
*/
fun <T> infiniteIteratorHelper(
initialCollection: Collection<T>,
initialIterator: Iterator<T>,
onNextItem: (T) -> Unit = {},
onIteratorRecreation: (Iterator<T>) -> Unit
): T {
return if (initialIterator.hasNext()) {
initialIterator.next().also(onNextItem)
} else {
val newIterator = initialCollection.iterator()
onIteratorRecreation(newIterator)
newIterator.next().also(onNextItem)
}
}
@Deprecated("Print this points")
private fun printSet(set: Set<Point>) {
val height = set.height()
println()
for (i in height downTo 0) {
print("|")
repeat(7) {
val symbol = if (Point(it, i.toInt()) in set) "%" else "."
print(symbol)
}
print("|")
println()
}
println()
}
private fun Collection<Point>.height(): Long = maxOf { it.y }.toLong()
private val figuresPattern = listOf(
FigureToCreate.HORIZONTAL_LINE,
FigureToCreate.CROSS,
FigureToCreate.TRIANGLE,
FigureToCreate.VERTICAL_LINE,
FigureToCreate.SQUARE,
)
private fun runOneFigureToBottom(
fallenPoints: MutableSet<Point>,
getNextFigure: () -> FigureToCreate,
getNextMovement: () -> Movement,
): Pair<FigureToCreate, Movement> {
val heightPoint = fallenPoints.maxBy { it.y }
val newFigureToCreate = getNextFigure()
var newFigure = Figure.createFigure(newFigureToCreate, heightPoint)
var nextCommand: Movement
do {
nextCommand = getNextMovement()
newFigure = when (nextCommand) {
Movement.LEFT -> newFigure.moveLeft(fallenPoints)
Movement.RIGHT -> newFigure.moveRight(fallenPoints)
}
newFigure = newFigure.moveDown()
} while (!newFigure.isOverlapping(fallenPoints))
newFigure = newFigure.moveUp()
fallenPoints.addAll(newFigure.points)
return newFigureToCreate to nextCommand
}
private fun Set<Point>.sliceTopPoints() = List(7) { x ->
Point(
x = x,
y = filter { it.x == x }
.height()
.toInt()
)
}
private fun calculateTowerHeight(
initialFigure: Figure,
commands: List<Movement>,
figuresToCreate: List<FigureToCreate>,
count: Long,
calculateFinalAnswer: (
fallenPoints: Set<Point>,
newFigureToCreate: FigureToCreate,
nextCommand: Movement,
movementIndex: Int,
commands: List<Movement>,
counter: Long
) -> Long? = { _, _, _, _, _, _ -> null }
): Long {
val fallenPoints = mutableSetOf<Point>().also {
it.addAll(initialFigure.points)
}
var commandsIterator = commands.iterator()
var figuresIterator = figuresToCreate.iterator()
var counter = count
var movementIndex = 0
while (counter > 0) {
val (newFigureToCreate, nextCommand) = runOneFigureToBottom(
fallenPoints = fallenPoints,
getNextFigure = {
infiniteIteratorHelper(
figuresToCreate,
figuresIterator
) {
figuresIterator = it
}
},
getNextMovement = {
infiniteIteratorHelper(
initialCollection = commands,
initialIterator = commandsIterator,
onNextItem = {
movementIndex++
},
onIteratorRecreation = {
movementIndex = 0
commandsIterator = it
}
)
}
)
counter--
calculateFinalAnswer(
fallenPoints,
newFigureToCreate,
nextCommand,
movementIndex,
commands,
counter
)?.let {
return it
}
}
return fallenPoints.height()
}
private fun findHeightWithOffset(
fallenPoints: Set<Point>,
cycleFigure: Figure
): Long {
val topLevelPoints = fallenPoints.sliceTopPoints()
val result = topLevelPoints.map { point ->
val verticalPoint = cycleFigure.points.find { it.x == point.x }!!
point.copy(y = point.y - verticalPoint.y)
}
return result.height()
}
private fun calculateFinalAnswer(
mapCycleHelper: Map<CycleInfoModel, Pair<Long, Long>>,
currentCycleHelperModel: CycleInfoModel,
startAmountOfSteps: Long,
currentStep: Long,
commands: List<Movement>,
fallenPoints: Set<Point>
): Long {
val (prevCycleItem, beforeCycleHeight) = mapCycleHelper[currentCycleHelperModel]!!
val currentCycleItem = startAmountOfSteps - currentStep
val cycleSize = currentCycleItem - prevCycleItem
val amountOfCycles = currentStep / cycleSize + 1
val remainingStepsAfterAllCycles = currentStep % cycleSize
val rotatedCommands = commands.toMutableList().also {
Collections.rotate(it, -currentCycleHelperModel.currentMovementIndex)
}
val rotatedFiguresToCreate = figuresPattern.toMutableList().also {
val index = it.indexOf(currentCycleHelperModel.currentFallingFigure)
Collections.rotate(it, -index - 1)
}
val remainingTowerHeight = calculateTowerHeight(
initialFigure = currentCycleHelperModel.figureWithNoOffset,
commands = rotatedCommands,
figuresToCreate = rotatedFiguresToCreate,
count = remainingStepsAfterAllCycles
)
val heightWithOffset = findHeightWithOffset(
fallenPoints = fallenPoints,
cycleFigure = currentCycleHelperModel.figureWithNoOffset
)
val cycleHeight = heightWithOffset - beforeCycleHeight
return remainingTowerHeight +
beforeCycleHeight +
cycleHeight * amountOfCycles
}
fun main() {
fun part1(input: List<String>, count: Long = 2022): Long {
return calculateTowerHeight(
initialFigure = Figure.createFloor(),
commands = parseCommands(input),
figuresToCreate = figuresPattern,
count = count
)
}
fun part2(input: List<String>, count: Long = 1_000_000_000_000): Long {
val cycleHelper = mutableSetOf<CycleInfoModel>()
val mapCycleHelper = mutableMapOf<CycleInfoModel, Pair<Long, Long>>()
return calculateTowerHeight(
initialFigure = Figure.createFloor(),
commands = parseCommands(input),
figuresToCreate = figuresPattern,
count = count
) { fallenPoints, newFigureToCreate, nextCommand, movementIndex, commands, counter ->
val cycleItem = CycleInfoModel.findCycleHelper(
points = fallenPoints,
currentFallingFigure = newFigureToCreate,
prevMovement = nextCommand,
index = movementIndex
)
if (cycleHelper.contains(cycleItem)) {
calculateFinalAnswer(
mapCycleHelper = mapCycleHelper,
currentCycleHelperModel = cycleItem,
startAmountOfSteps = count,
currentStep = counter,
commands = commands,
fallenPoints = fallenPoints
)
} else {
cycleHelper.add(cycleItem)
mapCycleHelper[cycleItem] = (count - counter) to findHeightWithOffset(
fallenPoints = fallenPoints,
cycleFigure = cycleItem.figureWithNoOffset
)
null
}
}
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day17_test")
val part1Test = part1(testInput)
val part2Test = part2(testInput)
check(part1Test == 3068L)
check(part2Test == 1514285714288L)
val input = readInput("Day17")
println(part1(input))
println(part2(input))
}