AoC 2026: Day 2

Main thread for Day 2 of the 2026 advent of code. Feel free to discuss the challenge and ask questions. If particular discussions become large, we can break them off into new threads.

Notice that Advent of Code has changed it’s schedule and there will only be 12 days of puzzles this year.

Some Rules:

  1. Please try to keep spoilers within spoiler tags. This is done by typing [spoiler]text to be blurred[\spoiler]. If you have longer sections you can also use the [details=“Title”] tags.
  2. Have Fun

Day 2 Challenge

Templates:

Resources:

Part 1

Build a state machine.

Part 2

Best order would be “rrr…vvv…bbb…ggg…yyy…ooo”
So simply take number of r*6 + number of o*6 …
Then add the transition from r to o which is 5. and the other transitions.
Turs out to be

const nsteps: usize = (input_str.len - 5 - 1) * 6 + 5 * 5

The extra -1 is due to the \n char at the end, which needs to be subtracted.
This only works, if all six instructions appear at least once. So most of the examples don’t work with this formula.

1 Like

I do these with JavaScript first so that’s always fun trying to translate to Zig… I ended up doing today with comptime :eyes: spoilers: my solution here.

I had a similar solution to @SpinTensor with one difference I made the solution to part 2 a big generalizable to work with the examples, so I still had to do a loop

EDIT: Relevant snippet, my solution was quite clean. I had to build a Slice for the counts, there are also two simplifications, color lookup order in reverse order, and how much to jump

Relevant snippet for part 2
    const color_loop:[6]u8 = [6]u8{1, 6, 5, 4, 3, 2};
    for (color_loop) |color| {
        const count = counts[color];
        if (count > 0) {
            const init_move:u8 = if (color > curr_pos) color - curr_pos else (seq_length - curr_pos) + color;
            sum += init_move;
            sum += 6 * (count - 1);
            curr_pos = @as(u8, color);
        }
    }
2 Likes

Your inputs have all instructions? Mine didn’t have any gs :slight_smile:

1 Like

For part one:

A simple loop that remembers the previous state works without issue, just sum up all the distances between all current location-target location pairs.

For part two:
@SpinTensor’s solution is clever, but my input also had no bs.
So I made a general solution. The code for it ended up pretty ugly, but it works! *<:^)

Part 2 relevant snippet
var counts = countColors(program);

dist = 0;
var i: usize = 0;
while (i < counts.len) {
    var idx: u8 = @intCast(@mod(-@as(i8, @intCast(i)), 6));
    dist += 6 * counts[idx];

    current_color = intToColor(idx);

    i += 1;

    // find next non-zero color
    while (i < counts.len) : (i += 1) {
        idx = @intCast(@mod(-@as(i8, @intCast(i)), 6));
        if (counts[idx] != 0) break;
    }
    if (i == counts.len) break;

    const target = intToColor(idx);

    // add distance to that color
    dist += distance(current_color, target);
    // we already travelled to the first occurance of that color, so reduce its count by 1
    counts[idx] -= 1;
}

Here is a general solution:

Part2
pub fn maxDistance(prog: []const u8) usize {
    var colors_count: [6]u16 = @splat(0);
    for (prog) |instr| {
        switch (instr) {
            'r' => {
                colors_count[0] += 1;
            },
            'v' => {
                colors_count[1] += 1;
            },
            'b' => {
                colors_count[2] += 1;
            },
            'g' => {
                colors_count[3] += 1;
            },
            'y' => {
                colors_count[4] += 1;
            },
            'o' => {
                colors_count[5] += 1;
            },
            else => {},
        }
    }
    var pos: u8 = 'r';
    const short_prog = "rvbgyo";
    var max_dist: usize = 0;

    for (colors_count, 0..) |cc, ic| {
        if (cc > 0) {
            const trans_val = step(short_prog[ic], &pos).?;
            max_dist += (cc - 1) * 6 + trans_val;
        }
    }
    return max_dist;
}
where `step()` is my function from the first part, that simply computes the step from one color to another.

I did it like your part two except I adjusted it to work for any input (including the test cases) by figuring out the transition distances

rather shortish complete file for part 2
const std = @import("std");

pub fn process(_: std.mem.Allocator, input: []const u8) !usize {
    var ret: usize = 0;
    var curr: usize = 0;
    const order = "rvbgyo";
    for (order, 0..) |byte, i| {
        if (byte == '\n') break;
        const tally = std.mem.countScalar(u8, input, byte);
        if (tally > 0) {
            ret += 6 * (tally - 1) + 6 - (i - curr);
            curr = i;
        }
    }
    return ret;
}

test process {
    const inputs: []const []const u8 = &.{
        "rrbbyy",
        "rybryb",
        "vrvbvg",
        "groovyyoyo",
        "roygbvroygbvroygbv",
    };
    const expected: []const usize = &.{ 32, 32, 33, 55, 103 };
    for (inputs, expected) |input, expect| {
        const output = try process(undefined, input);
        try std.testing.expectEqual(expect, output);
    }
}
1 Like