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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


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