Skip to content

Advent of Code 2026 Day 2: Fulminating Farmland

from advent import Counter, pairwise, parse

program = parse(2, list)[0]
points = "roygbv"
pos = {c: i for i, c in enumerate(points)}
start = 'r'


def distance(src, dst):
    return (pos[dst] - pos[src]) % len(points) or len(points)


def part1(program=program, distance=distance):
    return sum([distance(src, dst) for src, dst in pairwise([start] + program)])


def part2():
    counts = Counter(program)
    max_prog = [c for c in [start] + list(reversed(points[1:])) if counts[c]]

    def dist(src, dst):
        return distance(src, dst) + distance(dst, dst) * (counts[dst] - 1)

    return part1(max_prog, dist)


assert part1() == 3969
assert part2() == 6463

Problem

We're given that waypoints appear in a repeating pattern: roygbvroygbv..., and that the robot starts at r. The input is a string of letters indicating which waypoint to travel to. For part one, the goal is to sum all the distances as we travel from one waypoint to the next. For part two, the goal is to rearrange the program so that the total distance travelled is maximized.

Input Parsing

program = parse(2, list)[0] provides us with a list of waypoints e.g. ['r', 'g', 'o', ... ]

----------------------------------------------------------------------------------------------------
day02.txt ➜ 1078 chars, 1 lines; first 1 lines:
----------------------------------------------------------------------------------------------------
rgoogrovyrvrrgyorvrrgroryorvyvrrovyroggoogogoygrovyyogyoovogrvrorogyro ... vgyrorrggvrgovyyoyvogryvg
----------------------------------------------------------------------------------------------------
parse(2) ➜ 1 entries:
----------------------------------------------------------------------------------------------------
(['r', 'g', 'o', 'o', 'g', 'r', 'o', 'v', 'y', 'r', 'v', 'r', 'r', 'g' ... g', 'r', 'y', 'v', 'g'],)
----------------------------------------------------------------------------------------------------

Solution

We'll use a simple string, points, to represent the repeating pattern, then we'll create a dict, pos, to store the indices of each letter. Lastly, we're told the robot starts at r:

points = "roygbv"
pos = {c: i for i, c in enumerate(points)}
start = 'r'

Our distance helper function returns the distance from one waypoint to another. Since the pattern repeats, we'll use modular arithmetic to "wrap around". This erroneously make the distance zero if we're travelling to the same waypoint, so in that case we add the number of points:

def distance(src, dst):
    return (pos[dst] - pos[src]) % len(points) or len(points)

With that in place, part one is simply summing all the distances from one waypoint to the next, starting with the start. We'll be reusing this function for part two, so it's parameterized with the program and the distance function. For part one, we default to the puzzle input, and the above distance function:

def part1(program=program, distance=distance):
    return sum([distance(src, dst) for src, dst in pairwise([start] + program)])

For part two, we're told to rearrange the waypoints to maximize the total distance travelled. Since the rearranged waypoints will be contiguous, we only need to know how many of each waypoint there are, so Counter(program) will provide that. The way to maximize the distance is to rearrange the waypoints in order of most to least distance, starting with the start waypoint. To order the waypoints after the start, we simply reverse their order, for example, after r, the next largest distance would be to v, then b, etc. We need to be wary of non-existent waypoints in our input, so a if counts[c] guard will skip any waypoint with a zero count.

counts = Counter(program)
max_prog = [c for c in [start] + list(reversed(points[1:])) if counts[c]]

We'll need a custom distance function for part two. For a set of N waypoints, the distance travelled is the distance from our current waypoint to the first one plus (N-1) times the distance from the waypoint to itself:

def dist(src, dst):
    return distance(src, dst) + distance(dst, dst) * (counts[dst] - 1)

With that in place, part two is simply calling part one with our new program and custom distance function:

return part1(max_prog, dist)

End