Advent of Code 2021 day 11
12/11/2021, 5:00 AMDavid Whittaker
12/11/2021, 5:38 AMgetAdjacentSides()
functionMichael Böiers
12/11/2021, 6:57 AMDavid Whittaker
12/11/2021, 7:12 AMcomputeIfPresent()
- neat functionephemient
12/11/2021, 9:12 AMcompute*
default methods on Map are additions in Java 8ephemient
12/11/2021, 9:13 AMDan Fingal-Surma
12/11/2021, 9:16 AMPaul Woitaschek
12/11/2021, 10:26 AMMichael Böiers
12/11/2021, 11:02 AMPaul Woitaschek
12/11/2021, 11:05 AMMichael de Kaste
12/11/2021, 12:04 PMMichael de Kaste
12/11/2021, 12:08 PMfor(var step = 0, steps == null || step < steps, step++){
...
}
just reads nicer to me than
var step = 0
while(steps == null || step < steps){
...
step++
}
StdlIb doesnt have open ranges so I can't do
for(step in 0..steps){
....
}
which seems to me the best way to do itkqr
12/11/2021, 1:36 PMMarcin Wisniowski
12/11/2021, 1:39 PMMarcin Wisniowski
12/11/2021, 1:40 PMgetAdjacent()
today, getAdjacentSides()
does not return diagonals. 😄 Here is my utils file: https://gitlab.com/Nohus/adventofcode2021/-/blob/master/src/main/kotlin/utils/Geometry.ktMichael de Kaste
12/11/2021, 2:09 PMDan Fingal-Surma
12/11/2021, 5:11 PMmapIndexed
on a StringNir
12/11/2021, 5:15 PMNir
12/11/2021, 5:15 PMcount
that's a sequence, and counts forever? Python has this in itertools, it was my first thought.Paul Woitaschek
12/11/2021, 5:16 PMNir
12/11/2021, 5:17 PMNir
12/11/2021, 5:17 PMfun part1() = getInputs().let { inputs -> (1..100).sumOf { inputs.step() } }
Nir
12/11/2021, 5:17 PMNir
12/11/2021, 5:24 PMNir
12/11/2021, 5:25 PMfun getInputs() = (aocDataDir / "2021" / "day11.txt").useLines { lines ->
lines.map { line -> line.mapTo(mutableListOf()) { it.toString().toInt() } }.toList()
}
fun List<List<Int>>.getNeighbors(row: Int, col: Int) = listOf(
Pair(row-1, col), Pair(row+1, col), Pair(row, col-1), Pair(row, col+1),
Pair(row+1, col+1), Pair(row+1, col-1), Pair(row-1, col+1), Pair(row-1, col-1)
).filter { getOrNull(it.first)?.getOrNull(it.second) != null }
fun List<MutableList<Int>>.startAvalanche(row: Int, col: Int): Long {
if (this[row][col] == 10) {
return 0
}
++this[row][col]
if (this[row][col] != 10) {
return 0
}
return 1 + getNeighbors(row, col).sumOf { startAvalanche(it.first, it.second) }
}
fun List<MutableList<Int>>.step(): Long {
val totalFlashes = withIndex().sumOf { row ->
row.value.indices.sumOf { colIndex -> startAvalanche(row.index, colIndex) }
}
forEach { row -> row.indices.forEach { if (row[it] == 10) row[it] = 0 } }
return totalFlashes
}
fun part1() = getInputs().let { inputs -> (1..100).sumOf { inputs.step() } }
fun part2(): Long {
val inputs = getInputs()
val size = inputs.sumOf { it.size.toLong() }
var step = 1L
while (inputs.step() != size) {
++step
}
return step
}
fun main() {
println(part1())
println(part2())
}
Nir
12/11/2021, 5:33 PMfun part2(): Long {
val inputs = getInputs()
val size = inputs.sumOf { it.size.toLong() }
var step = 1L
while (inputs.step() != size) {
++step
}
return step
}
fun part2b(): Long {
val inputs = getInputs()
val size = inputs.sumOf { it.size.toLong() }
return generateSequence(1L) {
if (inputs.step() != size) it + 1 else null
}.last()
}
nkiesel
12/11/2021, 8:12 PMfor (step in 0 until steps)
though in this concrete case, for (step in 1..steps)
is even nicer.Michael de Kaste
12/11/2021, 9:08 PMephemient
12/11/2021, 9:23 PMfor (i in nullable.orEmpty())
or for (i in 0 until (count ?: 0))
. although you could of course use nullable?.forEach { i -> }
or repeat(count ?: 0) { i -> }
as wellphldavies
12/11/2021, 9:52 PMKiet
12/11/2021, 10:28 PMDan Fingal-Surma
12/11/2021, 10:50 PMsequence
— much bettergnu
12/12/2021, 1:17 AM