Advent of Code Day Two - 2026
Explainer for day two's puzzles, walking through my approach step by step in pseudocode, with my Python solution linked on GitHub
Introduction
This post is part of my Advent of Code 2026 series. Before committing to this article, I hope you, the reader, have solved or attempted the puzzle and are using this article as additional learning material to see how I went about solving mine. Whatever the case, happy reading 🎉
Well well well! I can't say I didn't see the increased difficulty coming, and besides, I got very busy with real-life stuff on day two and was only able to get a good look at it at night. By then, I think my brain was mostly done for the day. I slept on it and I solved it on the morning of day three.
The puzzle: In plain words
Part 1
The goal of the puzzle is to count the steps taken to walk through a repeating pattern of letters, given a program that decides which letter to find next. The letters are the colours of the rainbow: red, orange, yellow, green, blue and violet.
The repeating pattern:
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
An example program:
obbvb
From the example above, the robot, the agent following the program, would have to walk along the repeating pattern and count the steps taken to reach each code in the program. The robot starts on the first r, and every move takes at least one step, so if the next code matches the letter the robot is standing on, it has to walk a full cycle of 6 to reach the next one (that is what happens with the two bs in a row below).
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ initial robot position
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 1 to next orange waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 3 to next blue waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 6 to next blue waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 1 to next violet waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 5 to next blue waypoint
After the program is completed, the robot has travelled a total distance of 16. This example could be very easily completed by hand, but as is the nature of Advent of Code puzzles, the actual input for the problem is much longer.
Part 2
This part of the puzzle asks you to rearrange the codes in a program so that it produces the longest possible walk, without adding or removing any codes.
The solution: In pseudocode-ish Python
I structure my solution scripts so that they read from a file that contains the input and then perform the analysis on that input.
Step 1: Read input from a file
Here is a short excerpt of the input for the puzzle:
ogogbrrrbgovgrorgbvvgr...
It is the same kind of program as obbvb, just much longer. For step one, I read that input into a string.
Step 2 (Part 1): Implement a function that returns total distance walked
Attempt 1
The very first thing that stood out to me was that I needed a way to represent the repeating pattern from the series of 6 letters. I immediately went with the cycle iterator from Python's itertools module, and for a very short while it felt like I was on to something. My first implementation looked like this:
from itertools import cycle
def calculate_walk_distance(program: str) -> int:
base_pattern = "roygbv"
walks = 0
for code in program:
for waypoint in cycle(base_pattern):
if waypoint == code:
break
walks += 1
return walks
This was my very first attempt at the problem, but there are a few issues with this approach:
- Each time a code is matched with the waypoint, the inner loop starts a fresh cycle iterator of the repeating pattern, so every code is counted from
rinstead of from where the robot actually is - The code is compared with the waypoint before a step is taken, which would cause a code of
rto match on zero steps instead of six
Attempt 2 - My actual solution
From my mistakes in the first attempt, I needed a way to index into the cycle iterator so that after a match was made, the loop continued from the last waypoint. I did a bit of experimenting to see if the cycle iterator supported indexing and immediately found the answer to be no. So instead of using the builtin cycle iterator, I decided to use the modulus operator to keep the index into the waypoints bounded. If modular arithmetic is new to you, this introduction to modular arithmetic explains how the modulus operator can be used to simulate a cycle.
Because the robot starts at index 0, the total number of steps walked so far is also the robot's position, so the current waypoint is simply pattern[walks % 6].
initialize the walks count to zero
loop over the code in the program:
run an inner loop, preferably a while loop:
increment the walks by 1
look up the waypoint at pattern[walks % 6]
if the current waypoint matches the code:
end the inner loop and continue the outer loop
return the final value of the walks count
After doing some research, I now realize that the cycle iterator was a good idea and would have worked too. I just had to create the cycle once, outside the loop, like this:
walks = 0
waypoints = cycle("roygbv")
next(waypoints) # robot starts on 'r', so skip it
for code in program:
for waypoint in waypoints:
walks += 1
if waypoint == code:
break
Both versions give a total of 16 for the example.
Step 3 (Part 2): Rearrange the program for the longest walk or not
This part is just wow! In my first attempt I tried to reorder the program codes as I iterated over them. It got messy real quick, and I decided I didn't want to go down that path.
I did a bit of reading on Reddit and found a neat bit of maths. A single move can never be longer than a full cycle of 6 steps, so a program of N codes can never make the robot walk more than 6 × N. The robot only gets the full 6 steps when the next code is the same letter it is already standing on, though. Every letter other than r (where the robot starts) has to be reached for the first time from a different letter, and that move always falls at least one step short of a full cycle. With five letters other than r, at least 5 steps are lost.
You can lose exactly 5 by grouping identical codes together and visiting the groups in reverse order: all the rs first, then the vs, bs, gs, ys and finally the os. Each move to the previous letter in the pattern takes 5 steps, and every repeat of the same letter takes the full 6.
Here's the formula for the furthest distance a program can be rearranged to make the robot travel, assuming all six letters appear in the program:
furthest distance = 6 × (number of codes) − 5
If some letters never appear, subtract the number of different letters other than r instead of 5.
Conclusion
Day two wasn't as easy and straightforward as the first day, and it gave me a reason to iterate and try things out on paper, which I love. It also had me revisiting a few concepts I had not used in a while, like using the modulus operator to simulate a cycle. If you want to see the actual code, you can find my solution on GitHub.
Merry Christmas again 🎉✨