Skip to Content

Advent of Code 2025 - Day 12, in Kotlin - Christmas Tree Farm

Kotlin solutions to parts 1 and 2 of Advent of Code 2025, Day 12: 'Christmas Tree Farm '

Posted on

Well today, Day 12, is the last day of Advent of Code 2025. I always look forward to Advent of Code and always manage to learn a bunch of new things, and this year is no exception.

Today, I woke up and read the puzzle and immediately thought “There’s no way this is the real final puzzle, there’s either some trick to it or I’m going to spend the next 13 days solving this”. After parsing the data and doing some basic checks, it turns out there is a trick! See below for more.

If you’d rather just view the code, my GitHub Repository is here.

Puzzle Input

Most of the work today is in parsing, so let’s get started.

First, note that we’ll take our input as a single String, which makes it easier to identify the individual groupings of things to parse.

While you might expect to have to map each of the shapes of the presents into a List<String> or something like that, we really only need to know how many actual spaces each present takes up. So for presents, we can simply count the number of # we see and store them in order in a List<Int>

We’ll store data about the place the presents end up in a data class called TreeRegion (n.b. even though “Always take advantage of a portmanteau” is one of the rules I live by, I opted not to call this class Treegion. I feel it’s important to call that out). The TreeRegion class could have been a Pair<Int, List<Int>> but this felt clearer (and besides, it’s the last day of Advent of Code, so why not?). Anyway, TreeRegion contains the total area take up (height * width) and the counts of all the presents it wants, in order. It also has an of function in the companion to do the parsing, which once again uses substringBefore and substringAfter to pick through the input.

class Day12(input: String) {

    private val presents: List<Int> = input.split("\n\n")
        .dropLast(1)
        .map { present -> present.count { it == '#' } }

    private val treeRegions: List<TreeRegion> = input.split("\n\n")
        .last()
        .split("\n")
        .map { TreeRegion.of(it) }

    private data class TreeRegion(val area: Int, val counts: List<Int>) {

        companion object {
            fun of(input: String): TreeRegion =
                TreeRegion(
                    input.substringBefore("x").toInt() *
                    input.substringAfter("x").substringBefore(":").toInt(),
                    input.substringAfter(" ").split(" ").map { it.toInt() }
                )
        }
    }
}

⭐ Day 12, Part 1

The puzzle text can be found here.

The Huge Shortcut

We don’t have to do anything other than answer the question - “do all of these tiles even fit in this space”. We don’t need to know the actual shapes or their dimension. Simply finding out of all of the # fit in the area given for a region is enough. Basically, the simplest test we’d probably start with if we had to fully implement this for real. I’ll be honest, it never would have occurred to me to yolo-submit this answer, but I ran into spoilers (boo) and here we are.

Fun Fact: This does not work on the sample input, only the real input. Which I think is a nice fun twist to end the year with.

// In Day12

fun solvePart1(): Int =
    treeRegions.count { region ->
        region.counts.zip(presents).sumOf { (count, present) ->
            count * present
        } <= region.area
    }

Because the counts in each region and the presents are in the same order, we can zip them together and get the sumOf the counts and presents for each region. Once we can that test for a region we can count the number of regions where this test passes for our answer!

Star earned! Thanks for reading along with me for another year.

Further Reading

  1. Index of All Solutions - All posts and solutions for 2025, in Kotlin.
  2. My Github repo - Solutions and tests for each day.
  3. Solution - Full code for day 12
  4. Advent of Code - Come join in and do these challenges yourself!