Advent Of Code 2026 - Day 2

A farm with red wodden walls behind a small white fence. In the background is a robot following waypoints and some mountains.

My solution for day 2 could probably be optimized, because it feels more bruce force-y than I like, but I wanted to share some highlights.

First enum class introduced: I know a lot of people solving those puzzle stick to manipulating strings, but I find it easier to reason about a problem with some proper types. That’s why I introduced a Waypoint type:

enum class Waypoint(val value: Char) {
    Red('r'),
    Orange('o'),
    Yellow('y'),
    Green('g'),
    Blue('b'),
    Violet('v');

    companion object {
        // Convert char to Waypoint
        fun fromChar(char: Char) = Waypoint.entries.first { it.value == char }
    }
    
    // Compute the distance between 2 Waypoints 
    fun to(other: Waypoint): Int {
        if (other == this) return 6

        if (other.ordinal > this.ordinal) return other.ordinal - this.ordinal

        val max = entries.maxOf { it.ordinal }

        return max - ordinal + other.ordinal + 1
    }
}

I’ve also added some tests. Those puzzles are a prime example of a good case for TDD since they give you the expected output for a specific input. I always find it extremely useful, especially to make sure you aren’t breaking anything in part 2. Kotest remains my testing framework of choice because I like the way the assertions look.

class Day02Test : FunSpec({

    test("compute distance between waypoints") {
        Day02.Waypoint.Red.to(Day02.Waypoint.Red) shouldBe 6
        Day02.Waypoint.Red.to(Day02.Waypoint.Orange) shouldBe 1
        Day02.Waypoint.Orange.to(Day02.Waypoint.Blue) shouldBe 3
        Day02.Waypoint.Blue.to(Day02.Waypoint.Blue) shouldBe 6
        Day02.Waypoint.Blue.to(Day02.Waypoint.Violet) shouldBe 1
        Day02.Waypoint.Violet.to(Day02.Waypoint.Blue) shouldBe 5
    }

    test("run programs") {
        Day02.getDistance("obbvb".asSequence().map { Day02.Waypoint.fromChar(it) }) shouldBe 16
        Day02.getDistance("roygbv".asSequence().map { Day02.Waypoint.fromChar(it) }) shouldBe 11
        Day02.getDistance("vbgyor".asSequence().map { Day02.Waypoint.fromChar(it) }) shouldBe 30
        Day02.getDistance("vvvvvv".asSequence().map { Day02.Waypoint.fromChar(it) }) shouldBe 35
    }
})

Isolating the part that computes the distance between 2 waypoints makes the code fairly easy to read to get the total distance achieved when the robot runs a program.

fun getDistance(input: Sequence<Waypoint>): Int {
    val (_, distance) = input.fold(Waypoint.Red to 0) { (current, distance), waypoint ->
        waypoint to (distance + current.to(waypoint))
    }

    return distance
}

As I mentioned, I’m not happy with part 2 because it’s doing way more work than it should. I’m computing the distance between all the remaining points, mutating a string… but that’s the good part about having a proper setup with tests. I will be able to go back to it in the future to revisit and optimize it if I feel like it… but let’s be honest here, I probably won’t.

fun sortProgram(input: List<Waypoint>): List<Waypoint> {
    val remnaining = input.toMutableList()
    val sorted = mutableListOf<Waypoint>()
    var currentPosition = Waypoint.Red
    while (remnaining.isNotEmpty()) {
        val (waypoint) = remnaining.map { it to currentPosition.to(it) }.maxBy { (_, distance) -> distance }

        currentPosition = waypoint
        remnaining.remove(waypoint)
        sorted.add(waypoint)
    }

    return sorted
}

As for the illustration, I got a little carried away. I’ve been meaning to draw some bigger landscapes, working on making it less flat. I’m fairly happy with the result, even if that means that I’m now 2 days behind since I’m writing this on day 5.