13

Day 2: Fulminating Farmland

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
[-] eco_game@discuss.tchncs.de 6 points 16 hours ago* (last edited 9 hours ago)

Kotlin

I don't know if I've ever seen an easier part 2 than today. If anything, I assume part 2 makes it possible to use the grouping counts from yesterday and do some smart maths with that, but I'm too sleepy to come up with that right now.

Code is just mapping the chars to ints for easier math (although now that I think about, I could've also just kept them as chars) and then counting the difference between chars following each other.

Edit: I've improved my walk() method to be more Kotlin-like, but it does still "step" along the input:

private fun walk(instructions: List<Char>) = (listOf(start) + instructions)
    .map { waypoints.indexOf(it) }
    .zipWithNext { a, b -> b - a }
    .sumOf { if (it == 0) waypoints.size else it.mod(waypoints.size) }

Solution

class Day02 : Puzzle {

    private lateinit var instructions: List<Char>
    private val waypoints = charArrayOf('r', 'o', 'y', 'g', 'b', 'v')

    override fun readFile() {
        val input = readInputFromFile(2026, 2, false)
        instructions = input.lines().filter { it.isNotBlank() }.flatMap { it.toList() }
    }

    override fun solvePartOne(): String {
        return walk(instructions).toString()
    }

    override fun solvePartTwo(): String {
        return walk(instructions.sortedByDescending { if (it != 'r') waypoints.indexOf(it) else waypoints.size }).toString()
    }

    private fun walk(instructions: List<Char>): Long {
        val positions = instructions.map { waypoints.indexOf(it) }
        val first = positions.first()
        var sum = if (first > 0) first.toLong() else waypoints.size.toLong()
        for (pos in positions.indices) {
            if (pos == 0) continue
            val diff = positions[pos] - positions[pos - 1]
            sum += if (diff <= 0) {
                waypoints.size + diff
            } else {
                diff
            }
        }
        return sum
    }
}

full code on Codeberg

this post was submitted on 02 Dec 2026
13 points (100.0% liked)

Advent Of Code

1262 readers
48 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 2026

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 3 years ago
MODERATORS