generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.kt
181 lines (148 loc) · 4.57 KB
/
Day11.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
package year2021.`11`
import readInput
//private const val NUMBER = 5
private const val NUMBER = 10
private const val ENABLE_LOGS = false
private data class Point(
val x: Int,
val y: Int,
) {
override fun toString(): String = "[$x,$y]"
}
private fun Point.neighbours(): Set<Point> {
return setOf(
Point(x + 1, y),
Point(x - 1, y),
Point(x, y + 1),
Point(x, y - 1),
Point(x + 1, y + 1),
Point(x - 1, y - 1),
Point(x + 1, y - 1),
Point(x - 1, y + 1),
)
}
private fun printMap(initialMap: Map<Point, Int>, number: Int, enableLog: Boolean = ENABLE_LOGS) {
if (enableLog.not()) return
val sortedPoints = initialMap.keys.toList()
.sortedWith(compareBy({ it.y }, { it.x }))
sortedPoints.forEachIndexed { index, point ->
if (index % number == 0) {
println()
}
print(" " + initialMap[point])
}
println()
}
private fun parseMap(input: List<String>): Map<Point, Int> {
val mutableMap = mutableMapOf<Point, Int>()
input.forEachIndexed { y, line ->
line.split("")
.filter { it.isNotBlank() }
.forEachIndexed { x, value ->
mutableMap[Point(x, y)] = value.toInt()
}
}
return mutableMap
}
private fun mergeTwoMatrix(one: Map<Point, Int>, two: Map<Point, Int>): Map<Point, Int> {
return one.mapValues { (key, value) ->
value + two.getOrElse(key) { error("Illegal key $key") }
}
}
private fun step1(initialMap: Map<Point, Int>): Map<Point, Int> {
val newMap = mutableMapOf<Point, Int>()
initialMap.keys.forEach { currentPoint ->
newMap[currentPoint] = initialMap
.getOrElse(currentPoint) { error("Illegal key $currentPoint") }
.inc()
}
return newMap
}
private fun step2(initialMap: Map<Point, Int>, count: (Int) -> Unit = {}): Map<Point, Int> {
var currentMap = initialMap.toMutableMap()
var deltaMap = currentMap
val setOfFlashedPoints = mutableSetOf<Point>()
while (currentMap.any { it.value > 9 }) {
deltaMap = deltaMap
.mapValues { 0 }
.toMutableMap()
currentMap.forEach { (currentPoint, value) ->
if (value > 9 && currentPoint !in setOfFlashedPoints) {
// flash and increase neighbours
currentPoint.neighbours()
.filter { it in deltaMap }
.forEach { neighbour ->
deltaMap.computeIfPresent(neighbour) { _, value -> value.inc() }
}
setOfFlashedPoints.add(currentPoint)
}
}
if (ENABLE_LOGS) {
println("-----------".repeat(5))
println("currentMap")
printMap(currentMap, NUMBER)
println("Delta")
printMap(deltaMap, NUMBER)
}
currentMap = mergeTwoMatrix(currentMap, deltaMap)
.mapValues { (point, value) ->
if (value > 9 && point in setOfFlashedPoints) {
0
} else {
value
}
}
.toMutableMap()
if (ENABLE_LOGS) {
println("Merge Result")
printMap(currentMap, NUMBER)
}
}
return currentMap
.mapValues { (point, value) ->
if (point in setOfFlashedPoints) {
0
} else {
value
}
}
.toMutableMap()
.also {
count(setOfFlashedPoints.size)
if (ENABLE_LOGS) {
println("Final Result")
printMap(it, NUMBER)
println("-----------".repeat(5))
}
}
}
fun main() {
fun part1(input: List<String>): Int {
var parsedMap = parseMap(input)
var counter = 0
repeat(100) {
printMap(parsedMap, NUMBER)
parsedMap = step2(step1(parsedMap)) {
counter += it
}
}
return counter
}
fun part2(input: List<String>): Int {
var parsedMap = parseMap(input)
var counter = 0
while (parsedMap.all { it.value == 0 }.not()) {
parsedMap = step2(step1(parsedMap))
counter++
}
return counter
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day11_test")
val part1Test = part1(testInput)
println(part1Test)
check(part1Test == 1656)
val input = readInput("Day11")
println(part1(input))
println(part2(input))
}