@AoKSolution object Day04 { @JvmInline value class Grid(val data: List) { constructor(lines: List, m: Unit = Unit) : this(lines.map { it.toCharArray() }) inline val cols get() = data.indices inline val rows get() = data[0].indices operator fun get(x: Int, y: Int): Char? = data.getOrNull(y)?.getOrNull(x) operator fun set(x: Int, y: Int, c: Char) = run { data[y][x] = c } fun countNeighbours(x: Int, y: Int, c: Char): Int { var count = 0 for (dy in -1..1) for (dx in -1..1) if (dx != 0 || dy != 0) if (get(x + dx, y + dy) == c) count++ return count } } context(_: PuzzleInput) fun part1() = Grid(lines).let { grid -> var c = 0 for (y in grid.rows) { for (x in grid.cols) { if (grid[x, y] == '@') { if (grid.countNeighbours(x, y, '@') < 4) { c++ } } } } c } context(_: PuzzleInput) fun part2() = Grid(lines).let { grid -> var total = 0 while (true) { val reachable = buildSet { for (y in grid.rows) for (x in grid.cols) if (grid[x, y] == '@') { if (grid.countNeighbours(x, y, '@') < 4) { add(x to y) } } } if (reachable.isEmpty()) break for ((x, y) in reachable) grid[x, y] = 'x' total += reachable.size } total } }