Main thread for Day 5 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.
Parser: 83 lines.
State machine: 51 lines.
Part 1 loops: 25 lines.
Part 2 loop: 14 lines.
It was a chore. I am exhausted. I do not know why Eric likes this kind of puzzles. I guess it would be much easier in some scripting language with regexps.
I thought I would need nested loops for part 2. Apparently numbers.append() inside the loop over numbers was enough to find all the numbers.
Edit: I revisited it and cleaned up some mess. Now it feels much nicer.
I do not know why everyone seem to always use hashmaps. The number can not go higher than 9999. An array is enough.
part 1 and part 2
var sum: u32 = 0;
var numbers: [10000]bool = @splat(false);
numbers[0] = true;
var list: std.ArrayList(u32) = .empty;
defer list.deinit(gpa);
try list.append(gpa, 0);
for (0..3) |_| {
const olds = try list.toOwnedSlice(gpa);
defer gpa.free(olds);
for (olds) |old| {
for (buttons[0..len]) |b| {
const new = b.push(old);
if (!numbers[new]) {
numbers[new] = true;
sum += new;
try list.append(gpa, new);
}
}
}
}
try queue.putOne(io, .m(p, .part1, sum));
while (list.pop()) |screen| {
for (buttons[0..len]) |button| {
const number = button.push(screen);
if (number > 9999) continue;
if (!numbers[number]) {
numbers[number] = true;
sum += number;
try list.append(gpa, number);
}
}
}
try queue.putOne(io, .m(p, .part2, sum));