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:
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.
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
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.
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);
}
}
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;
}