@@ -19,7 +19,7 @@ fun EnergyLevel.step(): EnergyLevel {
19
19
}
20
20
}
21
21
22
- fun Point.toMinimalString (): String {
22
+ fun Point.toCompactString (): String {
23
23
return " ($row , $col )"
24
24
}
25
25
@@ -41,6 +41,12 @@ fun EnergyGrid.findAllPointsNeighboring(p: Point): List<Point> {
41
41
}.flatten().filterNotNull()
42
42
}
43
43
44
+ fun EnergyGrid.entryCount (): Int {
45
+ val rowCount = this .size
46
+ if (rowCount == 0 ) return 0
47
+ return rowCount * this [0 ].size
48
+ }
49
+
44
50
fun EnergyGrid.map (fn : ((Point , EnergyLevel ) -> EnergyLevel )? = null): EnergyGrid {
45
51
return this .mapIndexed { rowIdx, rowData ->
46
52
rowData.mapIndexed { colIdx, energyLevel ->
@@ -142,7 +148,7 @@ fun Simulation.step(): SimulationStepResult {
142
148
}
143
149
144
150
fun runSoluationPart1 (energyGrid : EnergyGrid , stepCount : Int = 10) {
145
- println (" Day 11 Solution: Part1 \n " )
151
+ println (" Day 11 Solution: Part 1 \n " )
146
152
147
153
var initSimulation = Simulation (energyGrid)
148
154
val initStep = Triple (0 , initSimulation, 0 )
@@ -162,10 +168,36 @@ fun runSoluationPart1(energyGrid: EnergyGrid, stepCount: Int = 10) {
162
168
println (" Total flashes: $totalFlashes " )
163
169
}
164
170
171
+ fun runSolutionPart2 (energyGrid : EnergyGrid ) {
172
+ println (" Day 11 Solution: Part 2\n " )
173
+
174
+ var initSimulation = Simulation (energyGrid)
175
+ val initStep = Triple (0 , initSimulation, - 1 )
176
+
177
+ val stepGenerator = generateSequence(initStep) { (step, simulation, prevFlashCount) ->
178
+ if (prevFlashCount == simulation.grid.entryCount()) {
179
+ null
180
+ } else {
181
+ val result = simulation.step()
182
+ Triple (step + 1 , result.simulation, result.flashedPoints.size)
183
+ }
184
+ }
185
+
186
+ val steps = stepGenerator.toList()
187
+
188
+ steps.forEach { (step, simulation, flashCount) ->
189
+ println (" After step $step " )
190
+ simulation.grid.prettyPrint()
191
+ println ()
192
+ }
193
+ }
194
+
195
+
165
196
fun main () {
166
- val energyGrid = File (" day11/src/main/resources/puzzleInput .txt" )
197
+ val energyGrid = File (" day11/src/main/resources/sampleInput .txt" )
167
198
.readLines()
168
199
.map { it.toList().map { c -> c.digitToInt() } }
169
200
170
201
runSoluationPart1(energyGrid, 100 )
202
+ // runSolutionPart2(energyGrid)
171
203
}
0 commit comments