day 2

Walking a colour cycle, then a greedy rearrangement

Updated Dec 2, 2026885 words5 min read

#. Problem

Advent of Code - 26’ day 2

— Day 2: Fulminating Farmland —

Part One

A farming robot travels along a line of waypoints whose colours repeat in a six-colour cycle: red (r), orange (o), yellow (y), green (g), blue (b) and violet (v).

roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...

The robot starts on a red waypoint. Each instruction in its program is a colour, and the robot moves to the nearest following waypoint of that colour. From red, o is distance 1, while r is a full lap, distance 6.

For example, the program obbvb travels 1 + 3 + 6 + 1 + 5 = 16.

  • roygbv travels a total distance of 11.
  • vbgyor travels a total distance of 30.
  • vvvvvv travels a total distance of 35.

Starting at a red waypoint, what total distance does the program cause the robot to travel?

Part Two

Unfortunately, you’ve confirmed the Elves’ suspicions: this program simply won’t take the robot far enough.

The Elves would just give the robot a longer program, but some of the keys on their keyboard are broken, so they can’t type the letters they need. The only thing they can do is rearrange the colors in the existing program.

However, they do know a trick for making the robot travel long distances. Whenever the robot is about to move, it should always get the instruction that would cause it to move the furthest. So, if the robot is at an orange waypoint, it would be best if the robot’s next instruction were o (for distance 6); if there aren’t any o instructions left, the next-best instruction would be r (for distance 5), and so on.

For example, rybryb could be rearranged into rrbbyy, giving a total distance of 32.

  • vrvbvg could be rearranged into rvvvbg, a total distance of 33.
  • groovyyoyo could be rearranged for a total distance of 55.
  • roygbvroygbvroygbv could be rearranged for a total distance of 103.

Starting at a red waypoint, what distance would the rearranged program cause the robot to travel?

1. My approach

Part One

Doubled the colour order so that indexOf always finds the next occurrence ahead of the current position, and counted the laps in carry.

const str = `roygbvroygbv`;

let idx = 0;
let carry = 0;

for (let c of input) {
    idx = str.indexOf(c, idx + 1);

    carry += Math.floor(idx / 6);
    idx %= 6;
}

console.log(carry * 6 + idx);

Part Two

The greedy rule always prefers the current colour (distance 6), so the robot uses up one colour completely before moving on. Once a colour runs out, the best next colour is the one just behind it in the cycle (distance 5). So the rearranged program is grouped in reverse order: rvbgyo.

Every instruction costs 6 except the first one of each new group. For my input that came out as 6n - 5:

console.log(input.length * 6 - 5);

2. Optimal approach

2.1. Part One without the doubled string

The distance to a colour can be computed directly with modular arithmetic. Shifting by + 5 and adding 1 afterwards maps “same colour” to 6 instead of 0.

const ORDER = 'roygbv';

let p = 0, total = 0;
for (const c of input) {
    const d = (ORDER.indexOf(c) - p + 5) % 6 + 1;
    total += d;
    p = (p + d) % 6;
}

console.log(total);

No lap counter is needed, because the total distance is just the sum of every step. Both versions give 16, 11, 30 and 35 for the examples.

2.2. Part Two for any input

6n - 5 is not a general formula, it only happened to fit my input. It fails on the first example: rybryb gives 6 * 6 - 5 = 31, but the answer is 32.

Where the 5 came from: my input has no y at all. The groups are visited as r → v → b → g → o. Switching to v, b and g costs 5 each (1 lost per switch), but from g the next colour behind is the missing y, so the robot has to go on to o at distance 4 (2 lost). That is 1 + 1 + 1 + 2 = 5.

A general solution simply simulates the greedy rule with a count of each colour. At every step, try distance 6 first, then 5, and so on, and take the first colour that still has instructions left.

const ORDER = 'roygbv';
const counts = [...ORDER].map(c => input.split(c).length - 1);

let p = 0, total = 0;
for (let left = input.length; left > 0; left--) {
    for (let d = 6; d >= 1; d--) {
        const k = (p + d) % 6;
        if (counts[k]) {
            counts[k]--;
            total += d;
            p = k;
            break;
        }
    }
}

console.log(total);

This gives the right answer for every example (32, 33, 55, 103) and for my input (5713).

3. Complexity analysis

Let n be the length of the program.

Solution Time complexity Space complexity
Part One O(n) O(1)
Part Two, formula O(1) O(1)
Part Two, simulation O(6n) = O(n) O(1)

The formula is faster, but it depends on which colours appear in the input. The simulation costs at most 6 checks per instruction, so it stays linear while working for any program.