← Advent of Code 2026
Day 2 · December 2, 2026

Fulminating Farmland

★★

Total distance is six per wrap-around plus where the robot ends.

Part 1
45 min after unlock
Part 2
1.1 h after unlock
Code
48 lines of Python
Techniques
modular arithmetic, sorting, greedy
Puzzle
adventofcode.com/2026/day/2

The puzzle

A robot drives along a track of colored waypoints that repeats red, orange, yellow, green, blue, violet, over and over. Its program is a string of color letters (r, o, y, g, b, v), and each instruction sends it forward to the next waypoint of that color. If it’s already on that color, it has to go all the way around: six waypoints. Part 1 asks how far the robot travels, starting on red.

Part 2 keeps the same instructions but rearranges them. Before each move, the robot takes whichever remaining instruction sends it farthest. Part 2 asks how far that rearranged program travels.

For a small example, take the program:

ygob

Starting on red, the robot moves 2 to yellow, 1 to green, 4 around to orange and 3 to blue, for a total of 10. That’s part 1.

For part 2, the farthest move from red is blue, 4 away. From blue, the farthest is green, 5 away around the loop. Then yellow, 5, and orange, 5. The rearranged program is bgyo, and it travels 19.

How I solved it

I didn’t add up the steps one at a time. In part1 I map each color to its place in the cycle, 0 for red through 5 for violet, and walk the program keeping only the current position and a lap count. If the next color is at or behind where the robot is, it has to go around, so that’s another lap. The total distance is then six for every lap plus the final position:

for num in program_nums:
  if num <= pos:
    laps += 1
  pos = num

return laps*6 + pos

For part 2, taking each color from the far end of the cycle down gives the longest move every time, so part2 just sorts the instructions in descending order. Red is the awkward one: the robot starts on red, so a red instruction costs a full lap, but its value of 0 would sort it last. I gave red a temporary value of 6 so it sorts first, then set it back to 0 after sorting, which was easier than writing a custom sort comparator. The sorted program then goes through the same lap-counting loop as part 1.

The code

import argparse
import re

class Solution:
  filename_real_input = 'real_input.txt'
  filename_test_input = 'test_input.txt'
  
  @staticmethod
  def get_nums(string: str) -> list[int]:
    return list(map(int, re.findall(r'[-+]?\d+', string)))
  
  def __init__(self, test=False):
    self.filename = self.filename_test_input if test else self.filename_real_input
    self.file = open(self.filename,'r').read()
    self.lines = ['obbvb','rrbbyy'] if test else [self.file.splitlines()[0],self.file.splitlines()[0]] # different test inputs for parts 1 and 2
    
  def part1(self):
    # roygbv
    # 012345

    # 14454
    #   x x
    program = self.lines[0]
    key = {'r': 0, 'o': 1, 'y': 2, 'g': 3, 'b': 4, 'v': 5}
    program_nums = [key[p] for p in program]
    
    pos = 0
    laps = 0
    for num in program_nums:
      if num <= pos:
        laps += 1
      pos = num
    
    return laps*6 + pos
  
  def part2(self):
    orig_program = self.lines[1]
    sort = {'r': 6, 'o': 1, 'y': 2, 'g': 3, 'b': 4, 'v': 5}
    sorted_nums = [sort[p] for p in orig_program]
    sorted_nums.sort(reverse=True)
    program_nums = [0 if n == 6 else n for n in sorted_nums] # easier than writing a custom sort comparator

    # get distance for optimal program
    key = {'r': 0, 'o': 1, 'y': 2, 'g': 3, 'b': 4, 'v': 5}
    pos = 0
    laps = 0
    for num in program_nums:
      if num <= pos:
        laps += 1
      pos = num
    
    return laps*6 + pos

    print(self.lines[1])
    pass
  
if __name__ == '__main__':
  parser = argparse.ArgumentParser('Solution file')
  parser.add_argument('-part', required=True, type=int, help='Part (1/2)')
  parser.add_argument('-test', required=True, type=str, help='Test mode (True/False)')
  args = parser.parse_args()
  test = True if args.test in ['True','true'] else False
  solution = Solution(test=test)
  result = solution.part1() if args.part == 1 else solution.part2()
  print(result)
problems/2026/day_2/solution.py on GitHub ↗