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

Advent Of Code

1199 readers
3 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
[โ€“] chunkystyles@sopuli.xyz 2 points 2 weeks ago* (last edited 2 weeks ago)

Edit: looking at your code, I had forgotten about .indices. That would have made this a little easier to write.

I completely forgot to do the puzzle yesterday somehow. I struggled a bit on this one for a while because I'd used a <= 4 where I should have used a < 4. Just a complete brainfart of thinking, "It needs to be 4 or less". I wasted more time on that than I'd like to admit.

My first stab at this set all of the adjacency counts to 0, and that lead to a few rolls that had no rolls adjacent to them staying on the map by accident.

const val toiletPaperRoll = '@'
const val adjacentLimit = 4
var height = 0
var width = 0

fun main() {
    val input = getInput(4)
    val map = parseInput1(input)
    height = map.size
    width = map[0].size
    val adjacencyMap = createAdjacencyMap(map)
    var anyRemoved = true
    var count = 0
    while (anyRemoved) {
        anyRemoved = false
        for (y in 0..<height) {
            for (x in 0..<width) {
                if (adjacencyMap[y][x] in 0..<adjacentLimit) {
                    count++
                    anyRemoved = true
                    removeToiletPaperRoll(adjacencyMap, x, y)
                }
            }
        }
    }
    println(count)
}

fun parseInput1(input: String): List<List<Char>> = input
    .lines()
    .filter { it.isNotBlank() }
    .map { it.toCharArray().toList() }

fun createAdjacencyMap(map: List<List<Char>>): List<MutableList<Int>> {
    val adjacencyMap = List(height) { MutableList(width) { -1 } }
    for (y in 0..<height) {
        for (x in 0..<width) {
            if (map[y][x] != toiletPaperRoll) {
                continue
            }
            adjacencyMap[y][x]++
            for (y2 in y - 1..y + 1) {
                for (x2 in x - 1..x + 1) {
                    if (y == y2 && x == x2 || (y2 < 0 || x2 < 0 || y2 >= height || x2 >= width)) {
                        continue
                    }
                    if (map[y2][x2] == toiletPaperRoll) {
                        adjacencyMap[y2][x2]++
                    }
                }
            }
        }
    }
    return adjacencyMap
}

fun removeToiletPaperRoll(adjacencyMap: List<MutableList<Int>>, x: Int, y: Int) {
    adjacencyMap[y][x] = -1
    for (y2 in y - 1..y + 1) {
        for (x2 in x - 1..x + 1) {
            if (y == y2 && x == x2 || (y2 < 0 || x2 < 0 || y2 >= height || x2 >= width)) {
                continue
            }
            if (adjacencyMap[y2][x2] >= adjacentLimit) {
                adjacencyMap[y2][x2]--
            }
        }
    }
}