Skip to Content

Advent of Code 2026 - Day 3, in Kotlin - Foggy Forest

Kotlin solutions to parts 1 and 2 of Advent of Code 2026, Day 3: 'Foggy Forest'

Posted on

I really liked this puzzle! I got part 1 quickly and it took me a bit of time drawing pictures to figure out how to solve part 2, but I got there in the end.

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

Puzzle Input

We’re three days into Advent of Code and we already need a 2D point class. I’m tempted to just copy one of the many Point2D classes we’ve written over the years, but I’ll start out simple. If this grows into something more, we’ll add to it when we get there. For now, this is about as basic as we can get it (and I struggled with the idea that I should probably just use Pair<Int,Int>).

// In Point2D

data class Point2D(val x: Int, val y: Int)

We also need to pair up our trees (points), so we’ll create a quick and dirty pairs function on Iterable.

// In Extensions.kt

fun <T> Iterable<T>.pairs(): List<Pair<T, T>> =
    flatMapIndexed { idx, a ->
        drop(idx + 1).map { b -> a to b }
    }

And since we’re supposed to be talking about parsing our input in this section, let’s go do that…

class Day03(input: List<String>) {

    private val trees = parse(input)

    private fun parse(input: List<String>): Set<Point2D> =
        buildSet {
            input.forEachIndexed { y, row ->
                row.forEachIndexed { x, ch ->
                    if (ch == '#') {
                        add(Point2D(x, y))
                    }
                }
            }
        }
}

We use Kotlin’s buildSet which allows us to have what appears to be a side-effecting add call in the middle of two for loops, but still have an immutable Set<Point2D> at the end of the day. I love using this kind of function, best of both worlds!

⭐ Day 3, Part 1

The puzzle text can be found here.

To solve part 1, we’ll pair up the trees (originally I had sorted things here but it turns out we don’t really need to). If the trees are on the same y axis, that might mean that directly below them would be some trees forming a square. So we get the deltaX and see if the spots which would make a square are in the trees.

// In Day03

private fun solve(): Int =
    trees
        .pairs()
        .filter { (a, b) -> a.y == b.y }
        .count { (a, b) ->
            val deltaX = b.x - a.x
            Point2D(a.x, a.y - deltaX) in trees &&
            Point2D(b.x, b.y - deltaX) in trees
        }

Now we can call this to solve part 1.

// In Day03

fun solvePart1(): Int = solve()

Star earned! Onward!

⭐ Day 3, Part 2

The puzzle text can be found here.

OK, this gets a bit tricky and we’ll have to rewrite solve to handle both part 1 and part 2. Let’s look at the code first, and then I’ll explain it.

The first thing we’ll do is add a boolean flag called allowedRotation to hint the implementation that we might want to allow rotated squares. We’ll default this to false so part 1 continues to work. We need to change the filter to look for the case when we allow rotations by checking the x values of a and b (or else we’ll possibly count too many). We also need to take the deltaY, given that they won’t necessarily be the same in part 2. This also slightly alters where the possible corners of our square might be. Otherwise, the theory is the same - make up two points where we think they should be and check to see if they are in trees and count them if they both are.

// In Day03

private fun solve(allowRotation: Boolean = false): Int =
    trees
        .pairs()
        .filter { (a, b) -> a.y == b.y || (allowRotation && a.x < b.x)}
        .count { (a, b) ->
            val deltaX = b.x - a.x
            val deltaY = a.y - b.y
            Point2D(a.x - deltaY, a.y - deltaX) in trees &&
            Point2D(b.x - deltaY, b.y - deltaX) in trees
        }

And similarly to part 1, we can solve part 2 by allowing rotations when calling solve.

// In Day03

fun solvePart2(): Int = solve(true)

Star earned! See you tomorrow!

Further Reading

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