import java.io.* import kotlin.math.* import kotlin.system.* fun main() { var state = File("input_actual.txt").readLines() state.forEach { println(it) } do { println("\ntransform\n") val new = transform(state) new.forEach { println(it) } val isChanged = new != state state = new } while (isChanged) println(state.sumBy { it.count { it == '#' } }) } fun transform(old: List) = old.mapIndexed { y, line -> line.mapIndexed { x, char -> val neighbours = old.subList(max(y - 1, 0), min(y + 1, old.lastIndex) + 1).sumBy { it.substring(max(x - 1, 0)..min(x + 1, line.lastIndex)).count { it == '#' } }.let { if (char == '#') it - 1 else it } when { char == 'L' && neighbours == 0 -> '#' char == '#' && neighbours >= 4 -> 'L' else -> char } }.joinToString("") }