this post was submitted on 12 Dec 2025
14 points (100.0% liked)

Advent Of Code

1198 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 12: Christmas Tree Farm

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 3 points 6 days ago* (last edited 6 days ago) (1 children)

Kotlin

Looking at the puzzle, I knew that I had no clue how to solve it. So I came here to see if I was missing something or if there were any hints.

And the hint I saw was to do the simplest check possible, so I gave it a shot.

And that got the test input wrong, but I ran it against the real input anyway just to see if it was right. And it was.

I think if I had gone on my instincts and just tried to solve this, I could have gone around in circles for hours or days trying to get it right.

fun main() {
    val input = getInput(12)
    val (gifts, regions) = parseInput1(input)
    var total = 0
    for (i in regions.indices) {
        val totalAreaOfGifts = regions[i].gifts.mapIndexed { index, count -> count * gifts[index].area }.sum()
        if (totalAreaOfGifts <= regions[i].area) {
            total++
        }
    }
    println(total)
}

data class Gift(val shape: List<List<Char>>, val area: Int)

data class Region(val width: Int, val height: Int, val area: Int, val gifts: List<Int>)

fun parseInput1(input: String): Pair<List<Gift>, List<Region>> {
    val gifts: MutableList<Gift> = mutableListOf()
    val regions: MutableList<Region> = mutableListOf()
    val lines = input.lines()
    lines.forEachIndexed { index, line ->
        if (line.contains(":")) {
            if (line.contains("x")) {
                val split = line.split(" ")
                val shape = split.first().replace(":", "").split("x")
                val width = shape.first().toInt()
                val height = shape.last().toInt()
                regions.add(
                    Region(
                        width,
                        height,
                        width * height,
                        split.slice(1..<split.size).map { str -> str.toInt() })
                )
            } else {
                var nextBlankLineIndex = 0
                for (i in index + 1..<lines.size) {
                    if (lines[i].isBlank()) {
                        nextBlankLineIndex = i
                        break
                    }
                }
                val shape = lines.slice(index + 1..<nextBlankLineIndex).map { it.toCharArray().toList() }
                val area = shape.flatten().filter { it == '#' }.size
                gifts.add(Gift(shape, area))
            }
        }
    }
    return gifts to regions
}
[–] Pyro@programming.dev 2 points 5 days ago

I struggled with optimizing this puzzle and had to go online to figure it out too, so I'm glad my hint helped you out.