Advent of Code Day 2 places a robot on a field whose waypoints repeat in a cycle of six colours: red, orange, yellow, green, blue and violet (roygbv). The robot starts on a red waypoint. The program (your puzzle input) is a sequence of colours, and for each instruction the robot travels to the very next waypoint of that colour. When the instruction names the colour the robot is already on, the next waypoint of that colour is a full cycle of six steps away. The puzzle asks for the total distance travelled.

The puzzle input is published alongside this post as /aoc/2026/data/day2.txt, a single program of 1042 instructions, with the worked example from the problem statement in day2-example.txt. Complete solutions are available as day2.roc and day2.raku. The Raku snippet below is the whole file; the Roc snippet elides only the embedded input string.

The worked example, program obbvb, one step at a time:

step  from  to  distance  running total
  1   r     o       1          1
  2   o     b       3          4
  3   b     b       6         10
  4   b     v       1         11
  5   v     b       5         16

The final distance of 16 matches the problem statement. Three further examples from the statement, each verified by running the solution:

roygbv    -> 11
vbgyor    -> 30
vvvvvv    -> 35

The algorithm is two integers and one line of modular arithmetic per instruction: a position in the cycle and a running total. The interesting part of this post is again what each language makes you write down.

1Roc

The platform constraints described in the day 1 post have not changed: a bare .roc file runs on a bundled platform whose only capability is Echo.line!, so the input is embedded. Day 1’s input was one value per line and used the \\ multiline syntax. Day 2 is a single line of 1042 characters, so it embeds as one ordinary string.

The complete solution, with the input string shortened:

# The puzzle input: one program of 1042 instructions.
input = "gvvggogbvogbbrrvgvrb...vorgborvbgvvroggbrrrr"

# Waypoint index of each colour, by its byte value. The annotation
# widens the fold: the running total (thousands) would not fit in a byte.
idx : U8 -> U64
idx = |b| {
    match b {
        114 => 0  # r
        111 => 1  # o
        121 => 2  # y
        103 => 3  # g
        98 => 4   # b
        118 => 5  # v
        _ => 0
    }
}

main! = |_| {
    bytes = Str.to_utf8(input)
    final = List.fold(bytes, { pos: 0, total: 0 }, |state, b| {
        target = idx(b)
        # add 6 before the modulo so the subtraction never goes negative
        raw = (target + 6 - state.pos) % 6
        dist = if raw == 0 { 6 } else { raw }
        { pos: target, total: state.total + dist }
    })
    Echo.line!("answer: ${final.total.to_str()}\n")
    Ok({})
}

Run against the full input:

answer: 3756

1.1The fold under a different name

The first version of this program assumed that a list fold would be called walk, which is the name it carries in several other languages. Roc 0.1.0 disagrees twice over:

Num.to_u64 does not exist.

List.walk does not exist.
List is in scope, but it has no associated walk.

Both errors were informative. List.fold is the fold, as in day 1, and there is no conversion function named to_u64 at all. Numeric widening is not an API call in this language.

1.2Widening with an annotation

The literal cast that day 1 used, 1.U64, does not work on a variable:

Required record access uses .name, optional record access uses .?name,
and tuple access uses .0.

I found .U64 here.
Names that start with uppercase letters are used for tags, type names,
and module names in Roc.

What does work is a type annotation on its own line above the definition, which day 1 already noted is where Roc puts them:

idx : U8 -> U64
idx = |b| {
    match b {
        114 => 0
        ...
    }
}

The bytes coming out of Str.to_utf8 are U8; saying once that idx returns U64 types the whole fold from there. The position and the total both become U64 without either being mentioned again, and the decision about how wide the total should be is made before the program runs rather than discovered inside it.

1.3No parsing at all

Day 1 spent a section on U64.from_str and List.keep_oks, because the input was text that happened to contain numbers. Here the input is letters, and they turn out to be the wrong abstraction:

bytes = Str.to_utf8(input)
bytes: [114, 111, 121, 103]

Once the program is a list of byte values, the colour table is a match on integers and the lookup is arithmetic on small unsigned numbers. There is no string to split, no parse step to fail, and no line separator to discard.

1.4Where the six comes from

The distance calculation is two lines:

raw = (target + 6 - state.pos) % 6
dist = if raw == 0 { 6 } else { raw }

Adding 6 before the modulo keeps the subtraction non-negative, and the if returns the full cycle of six when the target colour equals the current one, which is the case the puzzle statement calls out explicitly.

2Raku

The same puzzle in Raku is the whole file:

# usage: raku day2.raku [path/to/input]   (default: ../data/day2.txt)
my $path = @*ARGS[0] // '../data/day2.txt';
my $program = $path.IO.slurp.trim;

my @cycle = <r o y g b v>;
my ($pos, $total) = (0, 0);

for $program.comb -> $color {
    my $target = @cycle.first(* eq $color, :k);
    my $d = ($target - $pos) % 6 || 6;
    $total += $d;
    $pos = $target;
}

say $total;
$ raku day2.raku
3756
$ raku day2.raku ../data/day2-example.txt
16

2.1comb and first

.comb splits the program into graphemes, and the cycle is an ordinary list of strings. The lookup, @cycle.first(* eq $color, :k), asks for the index of the first element matching the colour, which is the entire colour table. Raku needs no annotation of what colour or index means because the question is phrased directly: which position in the list holds this value?

2.2Modulo without a sign

The step is a single expression:

my $d = ($target - $pos) % 6 || 6;

Raku’s % returns a non-negative remainder, so the jump backwards from violet to blue, (4 - 5) % 6, is 5 rather than -1. The worked example exercises that exact step, which is one reason it is worth running. The || 6 covers the same-colour case, where the modulo yields 0.

3The answer

Both languages agree:

$ raku day2.raku
3756
$ roc run day2.roc
answer: 3756

All four examples from the statement produce their published totals, and a third implementation that walked the track waypoint by waypoint, one step at a time instead of arithmetic on indices, produced the same 3756 for the real input.

The distribution of step sizes is close to uniform, as the mod-6 structure suggests, with a bias towards full cycles:

distance 1: 178
distance 2: 141
distance 3: 202
distance 4: 144
distance 5: 148
distance 6: 229

The mean step is 3.60 waypoint gaps across the 1042 instructions, and distance 6, the same-colour jump, is the most common single outcome: the colour repeats, so the robot walks a whole cycle.

On timings, the compiled Roc binary settles at 3 to 5 milliseconds per run once the first, cold-cache run has paged it in. Raku, including interpreter startup, takes 95 to 97 milliseconds warm. For 1042 instructions neither number is about the work; it is the cost of the language around it.

4Part two

Part two keeps the program and the waypoints, but the colours in the program may be rearranged. The Elves’ trick is greedy: whenever the robot is about to move, spend whichever instruction remains that carries it furthest. The greed is safe because the choice is never ambiguous. From any waypoint the six colours give six different distances, one of each integer from 1 to 6, so among the colours still available there is always exactly one that moves furthest. The worked example from the statement, rybryb, rearranges into rrbbyy for a total of 32.

The other examples from the statement, each a real run of the same script:

rybryb               -> 32
vrvbvg               -> 33
groovyyoyo           -> 55
roygbvroygbvroygbv   -> 103

The solution is a second pair of programs, day2-part2.roc and day2-part2.raku. Both count the colours in the input once, then spend them one at a time.

In Raku the loop stays where part one kept its state, a Bag of what remains, and the choice is max over a mapped block:

my %left = $path.IO.slurp.trim.comb.Bag;

while %left.elems {
    my $c = %left.keys.max({ (@cycle.first(* eq $_, :k) - $pos) % 6 || 6 });
    my $d = (@cycle.first(* eq $c, :k) - $pos) % 6 || 6;
    $total += $d;
    $pos = @cycle.first(* eq $c, :k);
    %left{$c}--;
    %left{$c}:delete if %left{$c} == 0;
}
$ raku day2-part2.raku
6247

Roc has no loop to spend the colours in, so the same walk becomes a recursion that consumes its own tail until the dictionary of counts is empty:

go = |left, pos, total| {
    if Dict.is_empty(left) {
        total
    } else {
        choice = List.fold(Dict.to_list(left), { colour: 0, dist: 0 }, |best, entry| {
            colour = entry.0
            raw = (colour + 6 - pos) % 6
            dist = if raw == 0 { 6 } else { raw }
            if dist > best.dist { { colour: colour, dist: dist } } else { best }
        })
        next = match Dict.get(left, choice.colour) {
            Ok(1) => Dict.remove(left, choice.colour)
            Ok(n) => Dict.insert(left, choice.colour, n - 1)
            Err(_) => left
        }
        go(next, choice.colour, total + choice.dist)
    }
}
$ roc run day2-part2.roc
answer: 6247

Both agree: the rearranged program travels 6247, against the 3756 of the program as written.

5One fold, two sets of obligations

Both solutions are the same state machine: a fold carrying a position and a total, with a six-way lookup and a modulo in the middle. What differs is what each language obliges you to state.

Roc required three decisions before the program could run: that the fold is List.fold rather than walk, that the colour lookup is U8 -> U64, and that the same-colour case deserves an if. None of the three is deep, but each is written down, and the annotation means the width of the total is fixed in the source rather than inferred at the point of use.

Raku required none of them. The cycle is a list, the colour is an argument to first, and the step is one expression whose sign behaviour comes from the operator rather than from arithmetic arranged around it. The + 6 that Roc needs before its modulo exists in Raku as a property of %. That is the same distinction day 1 found in a different place: Roc supplies the mechanism and makes you wire it, Raku supplies the finished operation.

For a problem this small the wiring is milliseconds of work on either side. The difference is not speed. It is which facts about the program end up as text in the file, and which end up as assumptions you carry in your head.