Nime
Metadata
Table of contents

Advent of Code 2026 Day 2

Day 2: Fulminating Farmland

https://adventofcode.com/2026/day/2

CHOO, CHOO! The train arrives at a huge farm.

The farm has an infinite line of coloured waypoints: The waypoints appear in a repeating pattern of six colors: red (r), orange (o), yellow (y), green (g), blue (b), and violet (v).

The elves built a fancy robot that goes to the waypoint it is instructed to and tends to the crops. The robot starts at a red waypoint.

Todays input is a list of those instructions.

An example input looks like this:

input.txt
vbgyor

Going to the next waypoint in that infinitely repeating pattern takes one step. So going from r to o takes 1 step, going from r to r causes the robot to take 6 steps (it wrapped around the pattern list).

Part 1

The question asks the number of steps the robot took after following all instructions.

I listed the waypoint colour pattern in a list.

Each colour maps to an index in that list.

I keep track of the index the robot is currently at in that pattern, curr. Because the robot starts at the first colour in that pattern, r, the starting index is 0.

If the next waypoint is ahead in the list, the robot takes next - curr steps. If the next waypoint is behind (or the same), the robot wraps around the list and takes 6 - curr + next steps.

day_02.rs
pub fn part_1(input: &str) -> usize {
let colours = ['r', 'o', 'y', 'g', 'b', 'v'];
let mut steps = 0;
let mut curr = 0;
for c in input.trim().chars() {
let next = colours.iter().position(|&x| x == c).unwrap();
steps += if next > curr {
next - curr
} else {
6 - curr + next
};
curr = next;
}
steps
}

Part 2

Not enough steps! Make the robot take as much steps as you can by rearranging the instructions.

The question asks for the number of steps the robot took after following the rearranged instructions.

Per instruction, the robot can take at most 6 steps. That means an input of n instructions long has a maximum amount of steps of 6 * n. (This happens when every instruction is r).

If I repeat the same colour first, the robot moves 6 steps per instruction. When that colour runs out, I pick whichever remaining colour gives the most steps next. Repeat until no instructions are left.

In practice, I switch colours in the reverse order as the pattern. (r, then v, b, g, y, o). The robot always travels the longest distance possible to the next waypoint that way.

Each loss is the gap between the 2 chosen colour indices. The total loss is the same as the difference from the start (r) to the “worst” colour index. You take that total loss a little bit at a time by picking the best colour every switch, but when all is said and done, you still had to take the entire thing.

Said otherwise, no matter how many colours you switch through, the total loss is determined by the index of the last colour you pick.

  • Ending on o loses 5 steps
  • Ending on y loses 4 steps
  • Ending on g loses 3 steps
  • Ending on b loses 2 steps
  • Ending on v loses 1 step
  • Only having r loses 0 steps

Because we go in reverse order (r -> v -> b -> g -> y -> o), the last colour we end on is whichever colour appears earliest in ['o', 'y', 'g', 'b', 'v'].

(r is excluded since starting on r adds no loss).

Finding its index i in that list gives a loss of 5 - i.

day_02.rs
pub fn part_2(input: &str) -> usize {
let input = input.trim();
let loss = ['o', 'y', 'g', 'b', 'v']
.iter()
.position(|&c| input.contains(c))
.map_or(0, |i| 5 - i);
input.len() * 6 - loss
}

Final code

day_02.rs
pub fn part_1(input: &str) -> usize {
let colours = ['r', 'o', 'y', 'g', 'b', 'v'];
let mut steps = 0;
let mut curr = 0;
for c in input.trim().chars() {
let next = colours.iter().position(|&x| x == c).unwrap();
steps += if next > curr {
next - curr
} else {
6 - curr + next
};
curr = next;
}
steps
}
pub fn part_2(input: &str) -> usize {
let input = input.trim();
let loss = ['o', 'y', 'g', 'b', 'v']
.iter()
.position(|&c| input.contains(c))
.map_or(0, |i| 5 - i);
input.len() * 6 - loss
}