generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.kt
271 lines (228 loc) · 7.6 KB
/
Day14.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
package year2023.`14`
import readInput
import utils.printDebug
import utils.printlnDebug
private const val CURRENT_DAY = "14"
private data class Point(
val x: Int,
val y: Int,
) {
fun moveNorth(): Point = copy(y = y - 1)
fun moveEast(): Point = copy(x = x + 1)
fun moveSouth(): Point = copy(y = y + 1)
fun moveWest(): Point = copy(x = x - 1)
}
private fun parseMap(input: List<String>): Map<Point, String> {
val mutableMap = mutableMapOf<Point, String>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
when (value) {
"O" -> mutableMap[Point(x, y)] = value
"#" -> mutableMap[Point(x, y)] = value
"." -> mutableMap[Point(x, y)] = value
else -> error("Illegal value x:$x y:$y value:$value")
}
}
}
return mutableMap
}
// region Tilts
private val invalidList = listOf("O", "#", null)
private val tiltNorthCache = mutableMapOf<Map<Point, String>, Map<Point, String>>()
private fun tiltNorth(
maxX: Int,
maxY: Int,
map: Map<Point, String>
): Map<Point, String> {
val cachedValue = tiltNorthCache[map]
if (cachedValue != null) return cachedValue
val resMap = map.toMutableMap()
for (x in 0..maxX) {
for (y in 0..maxY) {
val currentPoint = Point(x, y)
val currentValue = resMap[Point(x, y)]
if (currentValue == "O") {
var buffPoint = currentPoint
while (buffPoint.y >= 0 && resMap[buffPoint.moveNorth()] !in invalidList) {
val prevPoint = buffPoint
val newPoint = prevPoint.moveNorth()
buffPoint = newPoint
resMap[prevPoint] = "."
resMap[newPoint] = "O"
}
}
}
}
tiltNorthCache[map] = resMap.toMap()
return resMap
}
private val tiltEastCache = mutableMapOf<Map<Point, String>, Map<Point, String>>()
private fun tiltEast(
maxX: Int,
maxY: Int,
map: Map<Point, String>,
): Map<Point, String> {
val cachedValue = tiltEastCache[map]
if (cachedValue != null) return cachedValue
val resMap = map.toMutableMap()
for (x in maxX downTo 0) {
for (y in maxY downTo 0) {
val currentPoint = Point(x, y)
val currentValue = resMap[Point(x, y)]
if (currentValue == "O") {
var buffPoint = currentPoint
while (buffPoint.x <= maxX && resMap[buffPoint.moveEast()] !in invalidList) {
val prevPoint = buffPoint
val newPoint = prevPoint.moveEast()
buffPoint = newPoint
resMap[prevPoint] = "."
resMap[newPoint] = "O"
}
}
}
}
tiltEastCache[map] = resMap.toMap()
return resMap
}
private val tiltSouthCache = mutableMapOf<Map<Point, String>, Map<Point, String>>()
private fun tiltSouth(
maxX: Int,
maxY: Int,
map: Map<Point, String>
): Map<Point, String> {
val cachedValue = tiltSouthCache[map]
if (cachedValue != null) return cachedValue
val resMap = map.toMutableMap()
for (x in maxX downTo 0) {
for (y in maxY downTo 0) {
val currentPoint = Point(x, y)
val currentValue = resMap[Point(x, y)]
if (currentValue == "O") {
var buffPoint = currentPoint
while (buffPoint.y <= maxY && resMap[buffPoint.moveSouth()] !in invalidList) {
val prevPoint = buffPoint
val newPoint = prevPoint.moveSouth()
buffPoint = newPoint
resMap[prevPoint] = "."
resMap[newPoint] = "O"
}
}
}
}
tiltSouthCache[map] = resMap.toMap()
return resMap
}
private val tiltWestCache = mutableMapOf<Map<Point, String>, Map<Point, String>>()
private fun tiltWest(
maxX: Int,
maxY: Int,
map: Map<Point, String>
): Map<Point, String> {
val cachedValue = tiltWestCache[map]
if (cachedValue != null) return cachedValue
val resMap = map.toMutableMap()
for (x in 0..maxX) {
for (y in maxY downTo 0) {
val currentPoint = Point(x, y)
val currentValue = resMap[Point(x, y)]
if (currentValue == "O") {
var buffPoint = currentPoint
while (buffPoint.x >= 0 && resMap[buffPoint.moveWest()] !in invalidList) {
val prevPoint = buffPoint
val newPoint = prevPoint.moveWest()
buffPoint = newPoint
resMap[prevPoint] = "."
resMap[newPoint] = "O"
}
}
}
}
tiltWestCache[map] = resMap.toMap()
return resMap
}
// endregion
private fun calculateAnswer(map: Map<Point, String>): Int {
val maxY = map.maxOf { it.key.y } + 1
var sum = 0
map.keys.forEach {
val currentValue = map[it] ?: error("AAAAAA")
if (currentValue == "O") {
sum += maxY - it.y
}
}
return sum
}
private fun printMap(
initialMap: Map<Point, String>
) {
val maxX = initialMap.maxOf { it.key.x }
val maxY = initialMap.maxOf { it.key.y }
for (y in 0..maxY) {
for (x in 0..maxX) {
val currentPoint = Point(x, y)
printDebug { "" + initialMap[currentPoint] }
}
printlnDebug { "" }
}
}
private fun performCycle(initialMap: Map<Point, String>): Map<Point, String> {
val maxX = initialMap.maxOf { it.key.x }
val maxY = initialMap.maxOf { it.key.y }
val northMap = tiltNorth(maxX, maxY, initialMap)
val westMap = tiltWest(maxX, maxY, northMap)
val southMap = tiltSouth(maxX, maxY, westMap)
return tiltEast(maxX, maxY, southMap)
}
fun main() {
fun part1(input: List<String>): Int {
val inputMap = parseMap(input)
val maxX = inputMap.maxOf { it.key.x }
val maxY = inputMap.maxOf { it.key.y }
val resMap = tiltNorth(maxX, maxY, inputMap)
return calculateAnswer(resMap)
}
fun part2(input: List<String>): Int {
val inputMap = parseMap(input)
printlnDebug { "Initial Map" }
printMap(inputMap)
var currentMap = inputMap
val listOf = mutableListOf<Map<Point, String>>()
var wasCycleFound = false
var currentCount = 1000000000
while (currentCount > 0) {
currentMap = performCycle(currentMap)
if (wasCycleFound.not()) {
if (listOf.contains(currentMap)) {
val listSize = listOf.size
val cycleSize = listSize - listOf.indexOf(currentMap)
printlnDebug { "CYCLE FOUND $cycleSize" }
currentCount %= cycleSize
wasCycleFound = true
} else {
listOf.add(currentMap)
}
}
currentCount--
}
return calculateAnswer(currentMap)
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 136)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 64)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 113525)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 101292)
}