Fulminating Farmland
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.)
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.
...
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_distanceI 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.
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:
- At any time throughout the program, it would be best if the robot moved to
the same color it’s already on (for example,
rtor, orotoo). This would take 6 steps (a full loop), the longest possible distance between two colors. - If that’s not possible, the next best color to move to would be the previous
color in the pattern (for example,
rtov, orotor). This would take 5 steps, the second longest possible distance between two colors. - If that’s not possible, the next best color would be the second-to-previous
color (for example,
rtob, orotov). This would take 4 steps, the third longest possible distance between two colors. - If that’s not possible, the next best color would be the third-to-previous
color (for example,
rtog, orotob). This would take 3 steps, the fourth longest possible distance between two colors. - etc.
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).
...
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!