AoC 2026: Day 5

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:

  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 5 Challenge

Templates:

Resources:

2 Likes

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));

That was fun.

After implementing the button functionality it was quite simple. Store obtained values in a hash map. Iterate over hashmap and sum.

part 1
pub fn pressThreeButtons(microwave: Microwave, values: *std.AutoHashMap(usize, usize), cook_time: usize, nbuttons: u8) void {
    if (nbuttons == 0) {
        for (microwave.buttons.items) |button| {
            const val = Microwave.pressButton(cook_time, button);
            values.put(
                val,
                val,
            ) catch {
                unreachable;
            };
        }
    } else {
        for (microwave.buttons.items) |button| {
            const curr_cook_time = Microwave.pressButton(
                cook_time,
                button,
            );
            pressThreeButtons(
                microwave,
                values,
                curr_cook_time,
                nbuttons - 1,
            );
        }
    }
}
part 2
pub fn pressButtons(microwave: Microwave, values: *std.AutoHashMap(usize, usize), cook_time: usize) void {
    for (microwave.buttons.items) |button| {
        const curr_cook_time = Microwave.pressButton(
            cook_time,
            button,
        );
        if (curr_cook_time <= 9999) {
            if (values.get(curr_cook_time) == null) {
                values.put(
                    curr_cook_time,
                    curr_cook_time,
                ) catch {
                    unreachable;
                };
                pressButtons(microwave, values, curr_cook_time);
            }
        }
    }
}
1 Like

hahaha, I knew right away this was going to be a dynamic programming problem :smiling_face_with_horns: . really fun problem today!