this post was submitted on 04 Dec 2025
16 points (100.0% liked)

Advent Of Code

1197 readers
4 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 4: Printing Department

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] eco_game@discuss.tchncs.de 3 points 1 week ago (2 children)

Kotlin

Pretty simple solution, just plain count / remove the rolls until none can be removed anymore. I would've liked to try using imaginary numbers this year (due to this article), but sadly Kotlin doesn't natively support them and I was too lazy to use a library.

Solution

class Day04 : Puzzle {

    val grid = mutableListOf<MutableList<Char>>()

    override fun readFile() {
        val input = readInputFromFile("src/main/resources/a2025/day04.txt")
        for ((r, line) in input.lines().filter(String::isNotBlank).withIndex()) {
            grid.add(mutableListOf())
            for (char in line) {
                grid[r].add(char)
            }
        }
    }

    override fun solvePartOne(): String {
        return countRolls(grid).toString()
    }

    override fun solvePartTwo(): String {
        var sum = 0
        var removed = -1
        while (removed != 0) {
            // main grid is modified here, not the best but doesn't really matter
            // also no idea how to clone 2D list in Kotlin
            removed = countRolls(grid, true)
            sum += removed
        }
        return sum.toString()
    }

    private fun countRolls(grid: MutableList<MutableList<Char>>, removeRolls: Boolean = false): Int {
        val dr = listOf(-1, -1, -1, 0, 0, 1, 1, 1)
        val dc = listOf(-1, 0, 1, -1, 1, -1, 0, 1)

        var sum = 0
        for (r in grid.indices) {
            for (c in grid[r].indices) {
                if (grid[r][c] != '@') continue

                var neighborCount = 0
                for (i in dr.indices) {
                    if (gridGet(grid, r + dr[i], c + dc[i]) == '@') neighborCount++
                }
                if (neighborCount < 4) {
                    sum++
                    if (removeRolls) grid[r][c] = '.'
                }
            }
        }
        return sum
    }

    private fun gridGet(grid: List<List<Char>>, r: Int, c: Int, default: Char = '.'): Char {
        return if (r in grid.indices && c in grid[r].indices) {
            grid[r][c]
        } else {
            default
        }
    }
}

full code on Codeberg

[โ€“] Deebster@programming.dev 3 points 1 week ago

Ha, I've got that article half-read in a tab somewhere. Same problem here though - they're not in the standard library for anything I plan to use for AoC.

load more comments (1 replies)