Advent of Code 2026 - Day 2, in Kotlin - Fulminating Farmland
Kotlin solutions to parts 1 and 2 of Advent of Code 2026, Day 2: 'Fulminating Farmland'
Today is one of those days where I feel like if I had more time to focus on Advent of Code, I’d come up with a better solution.
If you’d rather just view the code, my GitHub Repository is here.
Puzzle Input
I’m sure there’s a less complex way to do this, but in my head this makes the most sense. Let’s create a Map<Char,IntArray> where the key is each of the waypoints, and the value is how many steps to one of the other waypoints in order (“roygbv”). Note that going from any waypoint to its next instance is a distance of 6.
class Day02(private val program: String) {
private val offsetMap = mapOf(
'r' to intArrayOf(6, 1, 2, 3, 4, 5),
'o' to intArrayOf(5, 6, 1, 2, 3, 4),
'y' to intArrayOf(4, 5, 6, 1, 2, 3),
'g' to intArrayOf(3, 4, 5, 6, 1, 2),
'b' to intArrayOf(2, 3, 4, 5, 6, 1),
'v' to intArrayOf(1, 2, 3, 4, 5, 6),
)
}
⭐ Day 2, Part 1
The puzzle text can be found here.
First, we need to create a solve function that calculates the overall duration the robot moves, given a path (the input). Because we need to start on r, we’ll prepend that to the input and compare each successive pair via zipWithNext. We can then take the sumOf all the costs from the offsetMap. We pick out the IntArray for the given “from” location (first) and look up the cost of the destination given that starting point.
// In Day02
private fun solve(input: String): Int =
"r$input"
.zipWithNext()
.sumOf { offsetMap.getValue(it.first)["roygbv".indexOf(it.second)] }
Now we can solve the puzzle directly, given our program.
// In Day02
fun solvePart1(): Int =
solve(program)
Star earned! Onward!
⭐ Day 2, Part 2
The puzzle text can be found here.
We’re told we need to rearrange the inputs so that the most expensive step is taken at each turn. To do that, we need to get all of the similar waypoints next to each other, so they’ll all cost the most. When transitioning between sets of identical waypoints, we want to pick the next most expensive transition. If we look at the offsetMap, we can eventually piece together that the most expensive order is “rvbgyo”. Anything else will cause us to minimize cost, which we don’t want in this case.
// In Day02
fun solvePart2(): Int {
val programs = program.groupBy { it }
val rearranged = "rvbgyo".map { programs[it]?.joinToString("") ?: "" }.joinToString("")
return solve(rearranged)
}
Again, this probably can be solved mathematically but I like the idea that we can use the solve function from Part 1 if we simply rearrange the waypoints. We can do that by grouping all of our inputs, picking them out of the map in the “rvbgyo” order, and then joining them together. This gives us a long string in the most expensive transition order at any step. Run that through solve and we’ve got our answer.
Star earned! See you tomorrow!
Further Reading
- Index of All Solutions - All posts and solutions for 2026, in Kotlin.
- My Github repo - Solutions and tests for each day.
- Solution - Full code for day 2
- Advent of Code - Come join in and do these challenges yourself!