Skip to main content

Day 2: Fulminating Farmland

by johnduffell

Puzzle description​

https://adventofcode.com/2026/day/2

AI declaration​

  • Scala code - No AI used.
  • This document - AI used for final review only.

Overview of part 1​

You have a repeating loop of six letters (the waypoints), and an arbitrary list of letters taken from those letters (the program).

At the start of the program, you are at the first waypoint, and as you read each letter, you move to the next instance of that letter.

Once you run out of letters in the program, you stop where you are.

The answer is the total number of waypoints that you pass (the distance).

This puzzle is quite a challenge in terms of off-by-one errors!

Parsing​

First we turn the program into a list of offsets - how far you have to walk from red to that colour waypoint. Note that r is assigned 6 rather than 0.

val program = "obbvb" // replace this with your input

val waypointOrder = "oygbvr" // shift r to the end so that it is at offset 6
val letterToOffset = waypointOrder.toList.zip(LazyList.from(1)).toMap

val indices = program.map(letterToOffset)
// gives ArraySeq(1, 4, 4, 5, 4)

The time complexity of parsing is O(n) as zip, toMap and map are linear.

Part 1 implementation​

To process the program, we need to go through the entire list in order, and we need to keep track of where we are up to. This means foldLeft is perfect, with the position as the accumulator.

On each iteration, we work out how far it is to the next waypoint of the correct colour. To do this, we look at where we are now (curPos) and where we need to be (nextColour). By subtracting, we can work out how many steps it is to nextColour.

The result of that is likely to be negative, and since we know we can't reverse, we need to bring it back in the range 1-6. Floor modulo works perfectly (unfortunately the built in % in scala does not work as we'd like here)

One final trap is that if we are already on the right colour waypoint, we need to walk six spaces, not 0. To get floorMod to produce 1-6 inclusive instead of 0-5 inclusive, we subtract 1 before calling it and add it back on again afterwards.

val startPos = 0
val result1 = indices.foldLeft(startPos) { (curPos, nextColour) =>
val increment = Math.floorMod(nextColour - curPos - 1, 6) + 1
curPos + increment
}

The time complexity of this solution is O(n) as foldLeft is linear. For a bonus exercise, one could implement a version that could work bottom up, rather like mergesort, and parallelise it.

Overview of part 2​

For the second part, the order no longer matters - we are free to reorder the program to get the maximum journey. However the length and letters of the program are fixed.

We know that the furthest possible single step is 6: when the program tells you to go to the waypoint of the same colour you're on. So we want to do that wherever possible.

If we have used all the commands of the starting colour, the next best is the one five steps away, and so on after using all of those. So the longest distance would be achieved by going backwards - i.e. doing all the rs followed by all the vs, then all the bs, etc.

This means that where n is the length of the program, the result is between 6n-5 and 6n, depending on which letter waypoint we finish on.

So to work out the answer, we work out the number of times we definitely wrap around, and then add on the number of steps to the final colour.

Part 2 implementation​

First we need to find the final colour, which since we are stepping backwards from r, is the shortest distance from r.

We find the lowest distance letter, defaulting to 6 if there are no letters in the program.

To find the minimum number of times that we cycle through all the colours, it must be one less than the number of letters in the program.

Then the final result is six times the number of cycles, plus the distance to the letter that we finally end up on.

val finalColour = indices.minOption.getOrElse(6)
val wrapArounds = indices.length - 1
val result2 = (wrapArounds * 6) + finalColour

The time complexity is O(n) as we call minOption on indices.

Here is the whole program in one place:

val waypointOrder = "oygbvr"
val letterToOffset = waypointOrder.toList.zip(LazyList.from(1)).toMap

def part1(input: String) = {
val indices = input.map(letterToOffset)

val startPos = 0
indices.foldLeft(startPos) { (curPos, nextColour) =>
val increment = Math.floorMod(nextColour - curPos - 1, 6) + 1
curPos + increment
}
}

def part2(input: String) = {
val indices = input.map(letterToOffset)

val finalColour = indices.minOption.getOrElse(6)
val wrapArounds = indices.length - 1
(wrapArounds * 6) + finalColour
}

val program = "obbvb" // replace this with your input
part1(program)
part2(program)

Solutions from the community​

Share your solution to the Scala community by editing this page. You can even write the whole article! Go here to volunteer