December ProofsEDITION / 2026

DAY 02 ADVENT OF CODE 2026

Fulminating
Farmland.

A farm that harvests thunder. A robot following colored waypoints. Six colors are all we need to measure a very long journey.

The thunder farm's repeating waypoint routeA small robot waits at red on a farm track. The waypoints repeat red, orange, yellow, green, blue, and violet across the fields. A storm cloud hangs above a thunder collector.roygbvroygbvroygbvSIX COLORS. ALWAYS FORWARD.
THE NEXT MATCH IS ALWAYS AHEAD

01 THE PUZZLE

A journey in six colors.

The train's first stop collects thunder from passing storms. Away from the rumbling crates, the Elves have a quieter problem: their farm robot waters, fertilizes, and weeds as it follows a program of colored waypoints. How far will that program take it?

The track repeats roygbv: red, orange, yellow, green, blue, violet. Start on red, then move to the next waypoint matching each instruction. Red to orange costs 1. Red to red costs 6: the waypoint under the robot does not count.

For Part Two, rearrange the same instructions. Before each move, choose the remaining color whose next waypoint is furthest ahead. Use each instruction exactly once, then measure the journey using the same movement rules.

Read the original puzzle on Advent of Code

Part One

Total distance traveled

Puzzle answer
3,681
Sample result · obbvb
16

1 + 3 + 6 + 1 + 5 = 16

Part Two

Choose the furthest waypoint

Puzzle answer
5,935
Sample result · rybryb
32

rybryb → rrbbyy = 32

02 THE INTERACTIVE LAB

Follow the next color.

The field keeps repeating. The robot only moves forward. Follow the original program in Part One, or rearrange the same instructions for the longest route in Part Two.

WAYPOINT WORKBENCH

Use r, o, y, g, b, and v. Spaces and line breaks are ignored.

Try a Part One example

Loading the sample walkthrough…

PERFORMANCE

How long did it take?

Measure both parts on the current program. Part Two includes rearranging the instructions.

03 THE IMPLEMENTATION

An infinite track, six positions.

Number the colors from 0 to 5. Subtract the current color's index from the destination's index, add 6, and take the remainder modulo 6. This gives the forward gap around one cycle. If that gap is 0, use 6 to reach the next occurrence.

Each instruction starts where the previous one ended, so its distance depends only on those two colors. Map instructions to jumps, then sum the jumps. There is no need to construct the repeating track: the calculation takes linear time in the program's length.

Part Two: count colors, then visit the buckets

Moving to the current color costs 6, farther than any other next waypoint. If that color remains in the program, keep choosing it until its instructions are exhausted. Starting at red, the furthest other color is violet, then blue, green, yellow, and orange. Skipping empty buckets preserves that same order.

Count the instructions with Remeda, then rebuild them in r → v → b → g → y → o order. The counts preserve every instruction while the bucket order implements the choice made at each move. For rybryb, two reds, two blues, and two yellows become rrbbyy: 6 + 6 + 4 + 6 + 4 + 6 = 32. Feed that new program into the Part One distance calculation.

base.tsShared solver
  1. LINE 1

    Bring in the functional tools

    Remeda maps instructions to jumps, adds their distances, counts colors, and expands the counts into a new program. None of these operations changes the input.

    import { countBy, flatMap, map, sumBy } from "remeda";
  2. LINES 3–4

    Describe one repeating cycle

    The tuple fixes the order red, orange, yellow, green, blue, violet. Its indices run from 0 to 5. Derive the Color type from this same tuple so the types and calculation share one definition.

    export const waypoints = ["r", "o", "y", "g", "b", "v"] as const;
    export type Color = (typeof waypoints)[number];
  3. LINES 6–16

    Read and validate the instructions

    Ignore whitespace so a program can wrap across lines. Reject an empty program or any character outside the six colors. Return a read-only list of validated instructions.

    export function parseProgram(input: string): readonly Color[] {
      const program = input.replace(/\s+/g, "");
      if (!program) throw new Error("Expected at least one waypoint instruction");
    
      return map([...program], (color) => {
        if (!waypoints.includes(color as Color)) {
          throw new Error(`Invalid waypoint: ${JSON.stringify(color)}. Use r, o, y, g, b, or v.`);
        }
        return color as Color;
      });
    }
  4. LINES 18–21

    Find the next occurrence, always ahead

    Subtract the starting color index from the destination index. Adding six before taking the remainder keeps backward differences positive. A zero remainder means the same color: use six rather than standing still.

    export function waypointDistance(from: Color, to: Color): number {
      return (waypoints.indexOf(to) - waypoints.indexOf(from) + waypoints.length)
        % waypoints.length || waypoints.length;
    }
  5. LINES 23–30

    Turn instructions into a route

    Each move starts at the preceding instruction’s color; the first starts at red. Map every instruction to a new jump record and sum the distances. Returning both jumps and total lets the walkthrough show the same work as the command line.

    export function analyzeProgram(program: readonly Color[]) {
      const steps = map(program, (to, index) => {
        const from = program[index - 1] ?? "r";
        return { from, to, distance: waypointDistance(from, to) };
      });
    
      return { steps, total: sumBy(steps, ({ distance }) => distance) };
    }
  6. LINES 32–34

    Return the Part One answer

    The command-line solution only needs the total. Keep the route calculation in one place so the answer and explanation cannot use different rules.

    export function totalDistance(program: readonly Color[]): number {
      return analyzeProgram(program).total;
    }
  7. LINES 36–42

    Rebuild the program in greedy order

    A matching color always gives the largest possible move, six, so consume every remaining copy before changing color. From red, the next-best colors run backward around the cycle: violet, blue, green, yellow, orange. Count the original colors, then expand those six buckets into a new list. Missing colors contribute no instructions. This is the Elves’ specified greedy rule, not a search over permutations.

    export function rearrangeProgram(program: readonly Color[]): readonly Color[] {
      const counts = countBy(program, (color) => color);
      const order = [waypoints[0], ...waypoints.slice(1).toReversed()];
      return flatMap(order, (color) =>
        Array.from({ length: counts[color] ?? 0 }, () => color)
      );
    }
  8. LINES 44–46

    Reuse the distance calculation for Part Two

    Rearrange the validated instructions, then pass the new list to the same Part One distance function. Counting and expansion both take linear time, with just six buckets to consider.

    export function rearrangedDistance(program: readonly Color[]): number {
      return totalDistance(rearrangeProgram(program));
    }
challenge-01.tsPart One entry point
  1. LINES 1–3

    Load the private input with Bun

    Import the shared functions and read input.txt next to this script. File access stays outside the pure calculation, and the website never imports this input file.

    import { parseProgram, totalDistance } from "./base";
    
    const input = await Bun.file(`${import.meta.dir}/input.txt`).text();
  2. LINE 4

    Print the original route’s distance

    Validate the program, calculate its total distance starting at red, and print the Part One answer. Run with bun run challenge-01.ts.

    console.log(totalDistance(parseProgram(input)));
challenge-02.tsPart Two entry point
  1. LINES 1–3

    Read the same original program

    Part Two uses the same private input as Part One. Import the shared parser and rearranged-distance wrapper rather than duplicating either calculation.

    import { parseProgram, rearrangedDistance } from "./base";
    
    const input = await Bun.file(`${import.meta.dir}/input.txt`).text();
  2. LINE 4

    Print the greedy route’s distance

    Validate the input, regroup its colors using the Elves’ greedy rule, and print the resulting distance. Run with bun run challenge-02.ts.

    console.log(rearrangedDistance(parseProgram(input)));

04 THE PUZZLE LOG

December 2026.

Published proofs, in order. New days will be added as they are solved.