generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.kt
306 lines (259 loc) · 8.63 KB
/
Day13.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
package year2023.`13`
import readInput
import utils.printDebug
import utils.printlnDebug
private const val CURRENT_DAY = "13"
private data class Point(
val x: Int,
val y: Int,
)
private fun parseIntoListsOfLists(input: List<String>): List<List<String>> {
val mutableRes = mutableListOf<List<String>>()
var currentList = mutableListOf<String>()
input.forEach {
if (it.isNotBlank()) {
currentList.add(it)
} else {
mutableRes.add(currentList)
currentList = mutableListOf()
}
}
mutableRes.add(currentList)
return mutableRes
}
private fun parseMap(input: List<String>): Set<Point> {
val mutableMap = mutableSetOf<Point>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
if (value == "#") {
mutableMap.add(Point(x, y))
}
}
}
return mutableMap
}
private fun Set<Point>.tryToFindMirrorsDifferentItems(): List<Set<Point>> {
val newSetList: MutableList<Set<Point>> = mutableListOf()
val maxX = maxOf { it.x }
val maxY = maxOf { it.y }
for (x in 0..maxX) {
for (y in 0..maxY) {
val point = Point(x, y)
val newSet = if (this.contains(point)) {
this - point
} else {
this + point
}
newSetList.add(newSet)
}
}
check(newSetList.all { it.size in setOf(size + 1, size - 1) })
return newSetList
}
private fun Set<Point>.tryToFindMirrors(prevAnswers: Set<Int>): Int? {
val maxX = maxOf { it.x }
val res: MutableSet<Int> = mutableSetOf()
for (i in 0..maxX) {
val mapToCheck = isMirroredHorizontally(i)
val isValid = verify(maxX, this, mapToCheck)
// if (isValid) println("X = $i isValid=$isValid")
if (isValid) res.add(i)
}
return (res - prevAnswers).firstOrNull()
}
private fun Set<Point>.tryToFindVerticalMirrors(prevAnswers: Set<Int>): Int? {
val maxY = maxOf { it.y }
val res: MutableSet<Int> = mutableSetOf()
for (i in 0..maxY) {
val mapToCheck = isMirroredVertically(i)
val isValid = verifyVertical(maxY, this, mapToCheck)
// if (isValid) println("Y = $i isValid=$isValid")
if (isValid) res.add(i)
}
return (res - prevAnswers).firstOrNull()
}
private fun verify(
maxX: Int,
points: Set<Point>,
validPoints: Map<Point, Boolean>,
): Boolean {
if (validPoints.values.distinct().size == 1) return false
val list = mutableListOf<Boolean>()
for (i in 0..maxX) {
val allSameForX = points.filter { it.x == i }
.map { validPoints[it]!! }
.distinctBy { it }
.size == 1
list.add(points.filter { it.x == i }
.map { validPoints[it]!! }
.all { it })
if (allSameForX.not()) return false
}
var count = 0
list.reduce { acc, b ->
if (acc == b) {
b
} else {
count++
b
}
}
return count == 1
}
private fun verifyVertical(
maxY: Int,
points: Set<Point>,
validPoints: Map<Point, Boolean>,
): Boolean {
if (validPoints.values.distinct().size == 1) return false
val list = mutableListOf<Boolean>()
for (i in 0..maxY) {
val allSameForY = points.filter { it.y == i }
.map { validPoints[it]!! }
.distinctBy { it }
.size == 1
list.add(points.filter { it.y == i }
.map { validPoints[it]!! }
.all { it })
if (allSameForY.not()) return false
}
var count = 0
list.reduce { acc, b ->
if (acc == b) {
b
} else {
count++
b
}
}
return count == 1
}
private fun Map<Point, Boolean>.height(): Long = maxOf { it.key.y }.toLong()
private fun Map<Point, Boolean>.width(): Long = maxOf { it.key.x }.toLong()
private fun printSet(set: Map<Point, Boolean>) {
val height = set.height()
printlnDebug { }
for (i in 0..height) {
printDebug { "|" }
repeat(set.width().toInt() + 1) {
val symbol = when (set[Point(it, i.toInt())]) {
true -> "1"
false -> "0"
null -> "."
}
printDebug { symbol }
}
printDebug { "|" }
printlnDebug { }
}
printlnDebug { }
}
private fun Set<Point>.isMirroredHorizontally(currentX: Int): Map<Point, Boolean> {
val result = associate {
if (it.x >= currentX) {
val xDelta = it.x - currentX
val pointToSearch = it.copy(x = currentX - xDelta - 1)
it to (pointToSearch in this)
} else {
val xDelta = currentX - it.x
val pointToSearch = it.copy(x = currentX + xDelta - 1)
it to (pointToSearch in this)
}
}
return result
}
private fun Set<Point>.isMirroredVertically(currentY: Int): Map<Point, Boolean> {
val result = associate {
if (it.y >= currentY) {
val xDelta = it.y - currentY
val pointToSearch = it.copy(y = currentY - xDelta - 1)
it to (pointToSearch in this)
} else {
val yDelta = currentY - it.y
val pointToSearch = it.copy(y = currentY + yDelta - 1)
it to (pointToSearch in this)
}
}
return result
}
fun main() {
fun part1(input: List<String>): Int {
var horizontalCounter = 0
var verticalCounter = 0
parseIntoListsOfLists(input)
.map { parseMap(it) }
.forEach {
printlnDebug { "NEW SET ARRIVED" }
printSet(it.associateWith { true })
val horizontal = it.tryToFindMirrors(emptySet())
val vertical = it.tryToFindVerticalMirrors(emptySet())
if (horizontal != null) {
horizontalCounter += horizontal
}
if (vertical != null) {
verticalCounter += vertical
}
}
return horizontalCounter + verticalCounter * 100
}
fun part2(input: List<String>): Int {
var horizontalCounter = 0
var verticalCounter = 0
parseIntoListsOfLists(input)
.map { parseMap(it) }
.forEach { ourSetOfPoints ->
printlnDebug { "NEW SET ARRIVED" }
val horizontal = ourSetOfPoints.tryToFindMirrors(emptySet())
val vertical = ourSetOfPoints.tryToFindVerticalMirrors(emptySet())
val resList = ourSetOfPoints.tryToFindMirrorsDifferentItems()
resList
.asSequence()
.map {
val newMirror = it.tryToFindMirrors(setOfNotNull(horizontal))
val newVMirror = it.tryToFindVerticalMirrors(setOfNotNull(vertical))
Triple(it, newMirror, newVMirror)
}
.filter { (_, hMirror, vMirror) ->
hMirror != null || vMirror != null
}
.filter { (_, hMirror, vMirror) ->
hMirror != horizontal || vMirror != vertical
}
.distinctBy { (_, hMirror, vMirror) -> hMirror to vMirror }
.onEach { (_, hMirror, vMirror) ->
printlnDebug { "H: $horizontal V:$vertical NEW_H:$hMirror NEW_V:$vMirror" }
if (hMirror != null && hMirror != horizontal) {
horizontalCounter += hMirror
}
if (vMirror != null && vMirror != vertical) {
verticalCounter += vMirror
}
}
.toList()
.ifEmpty {
printlnDebug { "NEW SET IS EMPTY - CHECK THIS" }
printSet(ourSetOfPoints.associateWith { true })
}
}
return horizontalCounter + verticalCounter * 100
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day${CURRENT_DAY}_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 405)// + 700)
val part2Test = part2(testInput)
println(part2Test)
check(part2Test == 400)
val input = readInput("Day$CURRENT_DAY")
// Part 1
val part1 = part1(input)
println(part1)
check(part1 == 34993)
// Part 2
val part2 = part2(input)
println(part2)
check(part2 == 29341)
}