AoC 2026 Day 2: Around and around we go

Source: Day 2: Fulminating Farmland

Full solution for today (spoilers!).

Part 1

Given an infinite cycle of ‘roygbv’ and a program of points to visit, advance to each point in turn. Count the total distance.

So for a first direct/simulationist solution to this problem, we’ll literally create the infinite cycle. Then for each character in the input, we’ll advance the cycle until we match and count as we go.

#[aoc::register]
fn part1_cycle(input: &str) -> impl Into<String> {
    // Create a literal infinite cycle of waypoints and just advance through it

    let mut waypoints = "roygbv".chars().cycle();
    waypoints.next();

    let mut dist = 0;

    for c in input.chars().take_while(|&c| c != '\n') {
        for n in waypoints.by_ref() {
            dist += 1;
            if n == c {
                break;
            }
        }
    }

    dist.to_string()
}

We could also do the same thing without having a separate dist variable by using an enumerate on the cycle–this one also uses find instead of iterating.

#[aoc::register]
fn part1_cycle_enumerate(input: &str) -> impl Into<String> {
    // Use enumerate to count distant and find to advance

    let mut waypoints = "roygbv".chars().cycle().enumerate().skip(1).peekable();

    for c in input.chars().take_while(|&c| c != '\n') {
        waypoints.find(|&(_, w)| c == w);
    }

    (waypoints.next().unwrap().0 - 1).to_string()
}

However, we also don’t need to maintain the cycle at all. We can always do this mathematically. There are three cases:

  • If you’re going to the same color, it’s always 6 (a full loop)
  • If you’re going ‘forwards’ it’s the difference in color
  • If you’re going ‘backwards’ (like o back to r) we go 6 minus the difference
#[aoc::register]
fn part1_math(input: &str) -> impl Into<String> {
    // Advancing is easy
    // Staying on the same color just goes one loop around
    // Going 'back' a color is 6 - the difference

    let mut current = 0;
    let mut dist = 0;

    for c in input.chars().take_while(|&c| c != '\n') {
        let next = index(c);
        let delta = if current == next {
            6
        } else if current > next {
            6 - current + next
        } else {
            next - current
        };

        dist += delta;
        current = next;
    }

    dist.to_string()
}

They do all end up doing the same thing:

$ just run 2

part1_cycle: 3527
part1_cycle_enumerate: 3527
part1_math: 3527

And are all super fast:

$ just bench 2

part1_cycle: 3.052µs ± 126ns [min: 2.833µs, max: 3.416µs, median: 3µs]
part1_cycle_enumerate: 4.424µs ± 270ns [min: 4µs, max: 5.875µs, median: 4.375µs]
part1_math: 1.087µs ± 19ns [min: 1.041µs, max: 1.167µs, median: 1.083µs]

It does make sense that not making the cycle is slightly faster, but when you’re at the 1-5µs range… 🤷

Part 2

For a given input, find the distance of the worst case.

This sounds like it should be an optimization problem, but really it’s not. What you have to realize is that the ‘worst’ case for any two points is staying on the same color. So the first thing we want to do is group all of each letter. Then the second thing is put them in order so that when we do switch, we go as far as possible, so r => v etc.

This ends up being equivalent to sorting with r first and the rest in reverse order:

#[aoc::register]
fn part2_sort(input: &str) -> impl Into<String> {
    // Sort so that we start with 'r' then go backwards through the colors

    let mut sorted: Vec<char> = input.chars().take_while(|&c| c != '\n').collect();
    sorted.sort_by(|a, b| {
        let a = index(*a).wrapping_sub(1);
        let b = index(*b).wrapping_sub(1);

        b.cmp(&a)
    });

    part1_math(&sorted.iter().collect::<String>()).into()
}

The wrapping_sub means that r gets usize.max() rather than 0 (forcing it to the other end). Then I’m literally just calling the fastest part1.

Another option would be to realize–every single waypoint we go to moves 6. Then we need to correct for each time we switch colors. If we hit all 6, this correction is always 5. But if we don’t…

#[aoc::register]
fn part2_math(input: &str) -> impl Into<String> {
    // 6 for each length
    // Then subtract for each transition we have to make, like r => v is only 5 not 6

    let mut seen: [bool; 6] = [false; 6];
    let mut count = 0;
    for c in input.chars().take_while(|&c| c != '\n') {
        seen[index(c)] = true;
        count += 1;
    }

    let correction = (1..6).find(|&i| seen[i]).map_or(0, |i| 6 - i);

    (6 * count - correction).to_string()
}

I’m honestly not sure which of these is more clear, but they do return the same thing:

$ just run 2

part2_math: 5851
part2_sort: 5851

And they’re still in the same order of magnitude:

$ just bench 2

part2_math: 1.487µs ± 116ns [min: 1.417µs, max: 2.625µs, median: 1.459µs]
part2_sort: 5.913µs ± 611ns [min: 5.333µs, max: 10.208µs, median: 6.125µs]

The difference here though is sorting is going to hit an $$O(n log(n))$$ runtime, while the math solution is $$O(n)$$ (to scan for each missing letter). So if the input gets really huge, the order matters more.

Visualization

Another weird one to visualize. Basically, scanning through the input on the top and the infinite cycle of waypoints on the bottom. If we had enough frames, you’d see the top stutter taking longer to go from r to r than r to o.

Benchmarks

$ just bench 2

part1_cycle: 3.052µs ± 126ns [min: 2.833µs, max: 3.416µs, median: 3µs]
part1_cycle_enumerate: 4.424µs ± 270ns [min: 4µs, max: 5.875µs, median: 4.375µs]
part1_math: 1.087µs ± 19ns [min: 1.041µs, max: 1.167µs, median: 1.083µs]
part2_math: 1.487µs ± 116ns [min: 1.417µs, max: 2.625µs, median: 1.459µs]
part2_sort: 5.913µs ± 611ns [min: 5.333µs, max: 10.208µs, median: 6.125µs]