Josiah Winslow solves Advent of Code

Fulminating Farmland

Published: 2026-12-02 Original Prompt

Part 1

We’re tracking the movement of a robot along a rainbow-colored path, with rules strikingly similar to Candy Land. How far will the robot go?

The rainbow-colored path will follow an infinitely repeating six-color pattern, which loops back to the start once it goes past the end. We could keep track of an index into this pattern using manual math, but I think itertools.cycle() from the standard library stands out as the obvious way to do it.

itertools.cycle() will take an iterable (such as "roygbv") and turn it into an infinitely repeating iterator of its items — which is exactly the behavior we want the colors along the path to have. We’ll use cycle() to create our cycling colors pattern, from which we’ll search for each waypoint the robot will move to. (We’ll also use next() to skip the initial waypoint, because colors will represent the waypoints ahead of the robot.)

2026\day02\solution.py
from itertools import cycle
COLORS = "roygbv"
class Solution(TextSolution):
def part_1(self) -> int:
program = self.input.strip()
# These will be the colored waypoints ahead of us
colors = cycle(COLORS)
next(colors) # Skip initial waypoint
...

At each instruction of the robot’s program, we want to find the distance to the next waypoint of that instruction’s color, and add it to a running total. We can find this distance with a simple yet carefully handled next() expression; enumerate() will associate each subsequent color with a distance (which should start at 1 instead of 0), and we want to find the distance when the color next matches the instruction.

2026\day02\solution.py
...
class Solution(TextSolution):
def part_1(self) -> int:
...
total_distance = 0
for instruction in program:
# Move to the next matching color, and add the distance
# NOTE We start checking from the waypoint in front of us,
# so the distance should start at 1.
total_distance += next(
distance for distance, color in enumerate(colors, start=1)
if color == instruction
)
return total_distance

I enjoyed writing these few lines; in my opinion, this approach ended up being rather Pythonic and beautiful.

Part 2

Our goal now is to rearrange the robot’s instructions to make it move the longest total distance. Other than that, the process of calculating this distance is the same, so we can factor it out into its own function.

2026\day02\solution.py
from collections.abc import Sequence
...
def program_distance(program: Sequence[str]) -> int:
... # Part 1 code from before
class Solution(TextSolution):
def part_1(self) -> int:
program = self.input.strip()
return program_distance(program)
def part_2(self) -> int:
... # TODO Add prepare-reordered-program code
return program_distance(program)

How do we order the instructions to make the robot move as far as possible? The prompt helpfully steps through a short example, and we’re going to use a few basic ideas from that example to come up with the best order:

So to maximize the robot’s total distance, the best order for its instructions is descending order by distance from the start. We can easily use sorted() to achieve this ordering, by setting reverse to True and setting key to the distance-calculating function from before (which, in this case, will operate on each instruction individually).

2026\day02\solution.py
...
class Solution(TextSolution):
...
def part_2(self) -> int:
# NOTE We want same colors grouped together to maximize full
# loops, and we want the groups in descending order of distance
# from the start to maximize the distance between groups.
program = sorted(
self.input.strip(),
key=program_distance,
reverse=True,
)
return program_distance(program)

The robot is now well-equipped to move as far along the farmland as it can go — and we barely had to change a thing from Part 1!