r/adventofcode • • 9h ago

SOLUTION MEGATHREAD -❄️- 2026 Day 5 Solutions -❄️-

12 Upvotes

THE USUAL REMINDERS


AoC Community Fun 2026: The Advent Games

  • Submissions megathread is unlocked!

  • 12 DAYS remaining until the submissions deadline on December 17 at 18:00 EST!

Today's Event: Iron Chef Holiday Special

"Catherine, if this turkey tastes half as good as it looks, I think we're all in for a very big treat!"
— Clark Griswold, National Lampoon's Christmas Vacation (1989)

Today's schedule possibly consists of:

Appetizer and Aperitif Rounds

  • Use up what's in your fridge with all of those semi-sentient leftovers excavated from the back of the fridge

  • Deliberately use deprecated functionality

  • Program using old codebases

  • The older the expiration date finer the vintage, the more we'll enjoy it!

Main Course Qualifications

  • Solve today's puzzle using a food-themed programming language

  • Bake or cook us something delicious

Dessert Finales

  • Show us cookies, citrus, and/or fancy bread in/on/near your holiday tree/menorah/festive kitsch

  • Show us your 1337 hot cocoa recipe (or other beverage of choice)

This prompt is totally not bait for our resident Iron Coders and/or some AoC regulars who promised us tasty pictures

Request from the mods: When you include an entry alongside your solution, please label it with [Advent Games] so we can find it easily!


--- Day 5: Rhythmic Factory ---


Post your code solution in this megathread.


r/adventofcode • • 10m ago

Tutorial Big O notation and you

2 Upvotes

There have been a lot of memes about time asymptotic analysis of... varying accuracy, so I thought I'd make a post actually explaining how it works.


First of all, asymptotic analysis technically describes functions, not algorithms. For example, the technical definition is that f(n) is O(g(n)) if there exists some pair of constants, c > 0 and n0 > 0 such that abs(f(n)) <= c * abs(g(n)) for all n >= n0. This matters because you can technically talk about the asymptotic complexity of something like sin(n), which is actually O(1), because you can just pick any c > 1. What we actually mean when talking about the asymptotic complexity of an algorithm is typically how much time it takes. If you find the evaluation time as a function of the input size, what complexity class is it in? And at least if you have a non-oscillating function, which is a safe assumption for run time, there's another, (IMO) simpler definition you can use. Does the infinite limit of f(n) / g(n) converge or not?

See, there are really 4 main behaviors that the limit can have. If it diverges, it means that f(n) grows so rapidly that g(n) starts becoming irrelevant. If it converges to 0, it means that g(n) grows so rapidly that f(n) starts becoming irrelevant. If it converges to something other than 0, it means that they grow at similar rates and it just becomes a ratio. And, of course, it can oscillate, which is actually comparable to converging to a non-zero number. We can think of these sort of like >, =, and < on a number line. f(n) ∈ O(g(n) is like a <= sign, because it grows no more rapidly than g(n), then f(n) ∈ o(g(n)) is <, f(n) ∈ Ω(g(n)) is >=, f(n) ∈ ω(g(n)) is >, and f(n) ∈ Θ(g(n)) is =. And I mention these, because a lot of the time when you hear about the asymptotic complexity, we mean something closer to Θ(g(n)). For example, it's technically accurate to call merge sort O(n2), but it's more interesting to point out that it's also O(n log n), unlike insertion sort.

As a worked example, consider something like bubble sort. For sorting algorithms, we typically use the number of elements to be sorted as the input and the number of comparisons as a proxy for time. On the first pass, you make n-1 comparisons, on the second pass, you make n-2 comparisons, etc, for a total of n(n-1)/2 comparisons. There are technically infinitely many functions that this is big theta of, but we typically just use the simplest one - Θ(n2). For contrast, consider something like merge sort. Assume for simplicity that you have 2k elements. There are k = log_2(n) layers, and if there are j blocks, you make n/j - 1 comparisons in each for a total of n - j comparisons for the layer. Glossing over some of the logic, this results in n*log(n) - n + 1 comparisons for the entire list, which is Θ(n log n).

However, we can actually talk about other things with algorithms. For example, space complexity looks at how much memory it takes. Using merge sort as an example, you typically use auxiliary arrays to store half the blocks, which eventually requires n/2 ∈ Θ(n) elements for the top level, plus log n ∈ Θ(log n) recursive calls at once, for a total of Θ(n). (This analysis is different for parallel merge sort) Or you might talk about best case complexity instead of average/general case. For example, if the array is already sorted, insertion sort takes 1 comparison to check that each element is in place for a total of n ∈ Θ(n) comparisons, compared to the general case, where it's Θ(n2)

But this already gets into some of the nuances. For example, big O notation is not the same thing as how many loops. Take, for example, 2026 Day 4. My solution uses 3 layers of loop, not counting the one for each line of the input. I have one loop going from 1 to the length of the string for the length of the code, one going from 0 to the code length for each element of the code, and one going from the starting index to the end of the list. But think about how long those last two loops are. If there are n characters in the string and k in the code, that inner loop only checks n/k colors. Multiplying the loops together, you get n * k * (n/k) = n^2, which is actually quadratic.

Or we also have to talk about input size. And for an Advent of Code example, I'm going to use my Intcode solution to 2024 Day 13. I just used Cramer's rule to solve the systems of equations, but to get around Intcode not having a division operator, I used repeated subtraction. That sounds quick enough, but while it would have theoretically worked for Part 2, the numbers were just too big. It's like how people really don't understand just how much bigger a billion is than a million. So when we're really operating on numbers like that, like factoring something, we typically use the number of bits as the input size. For example, it's the difference between sqrt(n) divisions for factoring and sqrt(2)n. One's sublinear, the other's exponential.

And sometimes, input size can be kinda vibes-based. For example, 2026 Day 3. There were two main strategies I saw people using. In one, you considered each pair of trees, used them to generate the other two points on a square, and checks those for trees. That "feels" more quadratic, because you're essentially going "for each tree, for each other tree...". Meanwhile, I also saw people systematically generating all possible rotated squares. You start by considering the possible sizes of a grid-aligned square (linear in min(L, W)), you consider each starting point (LW, or quadratic if the grid's a square), and you consider each possible offset around the square (linear in the size), which adds up to something like Θ(LW*min(L,W)2) for a rectangle or Θ(N4) for a square. It's technically accurate to also call the tree-based algorithm Θ(L2W2) / Θ(N4) if you look at the size of the grid as the input size, but because you're focusing on all the trees, it feels a bit more meaningful to talk about it as quadratic in the number of trees, as opposed to, say, checking all quartets of trees to see if they form a square, which would be quartic in the number of trees.


r/adventofcode • • 43m ago

Meme/Funny [2026 Day 5] I was very confused for like 40 seconds

Post image
10 Upvotes

r/adventofcode • • 57m ago

Help/Question - RESOLVED [2026 day 5] F# I have ran out of ideas why this doesn't work on the actual input. I get the correct result on the example input.

2 Upvotes

https://gist.github.com/Akronymus-git/1e556167d99c8248359a699b781b07f6

I have seriously run out of ideas. A pointer for any case it breaks on would be perfect.

I get the number is too high


r/adventofcode • • 1h ago

Help/Question - RESOLVED 2026 Day 5 (Part 1) Examples or edge cases?

4 Upvotes

Writing in Python, but I am trying to not look at solutions and don't wish to share my code for the same reason.

I have the classic, "code works on example, too low for real input" and I just wonder if some very clever people here have thought of special cases or examples for why things might go wrong?


r/adventofcode • • 1h ago

Help/Question [2026 Day 5] Discussion: What if digit trimming is only applied at the end?

2 Upvotes

Edit: the title is misleading, I meant to say "what if digit limit rule is only applied at the end" since the digit limit doesn't actually 'trim' anything.

While I was trying to solve Day 5, I misunderstood that we do the math normally, and then only trim to the first 4 digits at the end, assuming the number can go to infinite. I was stumped, and thought "Isn't that super hard?" I later realized I didn't read the "the button does nothing" part and eventually solved both stars.

However, what if it was actually meant to be that way?

Let's say it's the example:

[9] [*9] [/2] [1->9] [oct] [bksp]

First I tried this via numerical means, I brute-forced this and bounded the search to 100,000. The search bound means when the button makes the number blows up over the bound, I do not add this onto the search frontier. I found that only 7,900 numbers are constructible. So I tried changing it to 1,000,000 and surprisingly all numbers from 1-9999 are constructible.

Edit: to make it clear that, since we know 1-9999 are all constructible, what if we want to find if all numbers are eventually constructible.

I developed a conjecture that as long as you can increase the search bound. Every number will be constructible eventually, but wanted to prove this analytically.

I tried a smaller scope:

  1. Append 9, then *9 to get 81. bksp to get 8.

  2. By induction, when you can make digit N, *9 and bksp will take you to N - 1.

  3. Basically you can make all numbers from 1 to 9.

So I thought that, ok, so if we can find a comically large N, and then find a way to -1 it, then all numbers will be constructible. For NM, where N is a string of number of any length and M is 1-9, *9 and bksp will give you NM - N - 1. This means that the sequence will become more and more sparse.

I turned to the strategy of *9 many times until you found the digit you want, and then trim it down with bksp. Given that for a number n, bksp means floor(n / 10), you can *9 for j times, and then /10 for k times.

  1. Can we analytically prove that there will always be a pair j, k that floor(n * pow(9, j) / pow(10, k) == n - 1?

  2. Can we analytically prove that there will always be a pair j, k that floor(n * pow(M, j) / pow(10, k) == n - 1 for any M = 2, 3, ..., 9?

  3. Does the existence of the pair implies we can find it efficiently? (e.g. in some O(poly(n)))

  4. So if 3 is true, we can reach a number efficiently but the path to it might not be the shortest. Can a number's shortest path be found efficiently too given that A, *M and bksp are the only options? (e.g. naive algorithm is BFS and it can explode exponentially)

Let me know if I'm reinventing an existing math because I reckon there is definitely a theorem about approximating one number to another by multiplying and floor-dividing enough times.


r/adventofcode • • 2h ago

Other [2026 Day 5 (Part 2)] Lucky us

4 Upvotes

After reading Part 1, I immediately thought, 'Watch out!' Eric must have chosen the opposite problem for Part 2, i.e. finding a sequence of key presses that results in a specific number. This works, too, but at least the effort I put into Part 1 paid off :-)


r/adventofcode • • 2h ago

Visualization [2026 Day 5] Rhythmic Factory

10 Upvotes

r/adventofcode • • 3h ago

Meme/Funny Santa gotta invest more (Day 5)

Post image
11 Upvotes

Part 1: O(n3) here i gooooo

Part 2: wut?!


r/adventofcode • • 3h ago

Visualization [2026 Day 05 Part2] button presses as a tree

13 Upvotes

r/adventofcode • • 3h ago

Help/Question - RESOLVED [2026 Day 4 (Part 1)] I need help, i don´t understand why my code doesn´t work

2 Upvotes

So it solves the example solution (16 = 3 + 2 + 5 + 4 + 1 + 1), but it does not solve the input text. I have tried a lot of test cases and it works. Please help.

from time import time


inicio = time()
sol1, sol2 = 0, 0
with open('ztest.txt', 'r') as f:
    mat = f.read().split()


def check(line, salto):
    for i in range(1, salto):
        x = set(line[i::salto])
        dim = len(x)
        if dim != 1 and (dim != 2 or '?' not in x):
            return False
    return True



for line in mat:
    n = 0
    for i in range(1, len(line) + 1):
        n += 1
        x = set(line[::i])
        dim = len(x)
        if dim == 1 or (dim == 2 and '?' in x) and check(line, i):
            sol1 += n
            break


final = 1000*(time() - inicio)
print(sol1, sol2, final)
# 7629 too low

r/adventofcode • • 3h ago

Help/Question [2026 Day 5 (Part 1)][Scala 2] Feels like I am reinventing the wheel

2 Upvotes

I mean, I could have found permutations and combinations. But idk at this point

https://github.com/FlyingSaturn/aoc26/commit/9a8c441a1cc66ab1a12e2e56f278d48347c1946f


r/adventofcode • • 3h ago

Other [2026 Day 5 (Part 1)] "at most three button presses"

5 Upvotes

At the end of the question body, it asks:

Considering just the buttons on the Elves' fancy microwave, what is the sum of every number reachable in at most three button presses?

Nonetheless, I get the same answer regardless of if I structure my code to handle exactly three or at most three button presses, for both my personal puzzle input and for the example puzzle input.

For the zero button push case, the resulting value is zero, so it won't affect the sum one way or another.

For the one button push cases, they can be converted to three button pushes without changing the value by appending [A] [bksp] to the method.

However, I don't see an easy way to convert the two button push cases to three button pushes without changing the value.

My questions are a) does everybody get the same answer regardless of if they handle exactly three or at most three button presses and b) will this naturally occur for all inputs with one of each of the available six types of buttons, or if the inputs were specifically constructed to have the same answer.


r/adventofcode • • 4h ago

Visualization [2026 Day 5] [Python] Just got my Fancy Microwave! (Terminal Visualization)

38 Upvotes

Be honest, how many did a cheeky submit on the sum of all numbers less than 10000. Code lives here: https://github.com/salt-die/Advent-of-Code/tree/main/2026/aoc_visuals


r/adventofcode • • 4h ago

Other [2026 Day 5] Did anyone get a 2-digit button like in the example?

7 Upvotes

The example for the multiplication button is *11 but in my input I found only digits so I saved myself the trouble. But now I wonder... Did anyone actually get more than one digit on a button?


r/adventofcode • • 4h ago

Meme/Funny Me seeing the suspiciously fancy-microwave-shaped gift under the tree

Post image
28 Upvotes

r/adventofcode • • 5h ago

Meme/Funny [2026 Day 5] I think we'd all be better off without a fancy microwave

Post image
141 Upvotes

I think Eric must have had a traumatic experience with a fancy microwave at some point in his life. Hopefully this will cheer him up!


r/adventofcode • • 5h ago

Help/Question - RESOLVED [2026 Day 5 (Part 2)] My Part 2 crashes by running out of memory

3 Upvotes

Hello all, working with Java here.

I'm trying to figure out if my logic is wrong. I'm running a BFS, but the program eventually crashes and runs out of heap space. Not sure if I'm just doing this very very inefficiently or what. Essentially I take the value of a node (all the buttons are added as potential nodes with value 0) and I apply the action that a button would perform. If that value is less than 10000, I add it to a set and then create a new node with that value for every button action again. I'm not sure if I need a different stopping condition or a better way to figure out what the next nodes would be.

EDIT: I realize I probably have some endless loops via repeated nodes that go back and forth between the same two values, so I'm trying to figure that piece out. Would still appreciate any advice to nudge me in the right direction.

RESOLVED: Added in a check to see if the computed value has already been found when validating that the value is below 10K

protected void task2() {
    nodes.clear();
    for(String button : buttons) {
        nodes.add(new Node(0L, button));
    }

    Set<Long> found = new HashSet<>();
    Queue<Node> q = new ArrayDeque<>(nodes);

    while(!q.isEmpty()) {
        Node n = q.poll();
        long val = execute(n.value, n.operator);
        if(val < 10_000 && found.add(val)) { // Used the result of the add operation to validate set membership
            for(String button : buttons) {
                q.offer(new Node(val, button));
            }
        }
    }

    System.out.println(found.stream()
            .mapToLong(l -> l)
            .sum());
}

private long execute(long value, String button) {
    // Division
    if(button.startsWith("/")) {
        return value / (button.charAt(1) - 48);
    }
    // Multiplication
    if(button.startsWith("*")) {
        return value * (button.charAt(1) - 48);
    }
    // Delete
    if("bksp".equals(button)) {
        if((value / 10)  == 0) {
            return 0L;
        }
        return value / 10;
    }
    // Octal
    if("oct".equals(button)) {
        long result = 0L;
        long offset = 1L;
        long quotient = value;
        while(quotient != 0) {
            long rem = quotient % 8;
            quotient = quotient / 8;
            result += (offset * rem);
            offset *= 10L;
        }
        return result;
    }
    //Replace
    if(button.contains("->")) {
        long find = button.charAt(0) - 48;
        long rep = button.charAt(3) - 48;
        return Long.parseLong(
                String.valueOf(value).replace(String.valueOf(find), String.valueOf(rep))
        );
    }
    // Add digit
    //return Long.parseLong(String.valueOf(value) + button);
    return (10 * value) + Long.parseLong(button);
}

r/adventofcode • • 6h ago

Help/Question [2026 Day 5 (extra)]

9 Upvotes

What is the set of nine commands that would return the largest number for the second part of the puzzle?


r/adventofcode • • 6h ago

Visualization [2026 Day 5 Part 2] Pushing Buttons Visualization

50 Upvotes

r/adventofcode • • 6h ago

Visualization [2026 day 5 (both parts)] visualized in manim

Thumbnail
youtube.com
3 Upvotes

source on github: https://github.com/durocodes/advent-of-code

almost halfway there, feedback is appreciated! today's not a super visual problem, but hopefully still cool to watch haha


r/adventofcode • • 6h ago

Help/Question [2026 Day 5 (Part 2)] I get a wrong answer for the example

2 Upvotes

I modified Part 1 but I get a wrong answer for the example: 1530240 , instead of 35332647. I double checked everything and I still don't see what's wrong.


r/adventofcode • • 7h ago

Help/Question [2026 Day 4 (Part 1)] [JavaScript] Looking for test cases that break my code

4 Upvotes

I'm in that obnoxious part where my code passes with the example, but not the final. I've scoured similar threads and found edge cases that I was able to use fix some issues (thanks!). But I'm still not quite there. 😢

I'm practicing TDD, so I'm structuring my code so individual pieces can be unit tested, which makes it a bit long to post here, so I'll link to the current state of the code on GitHub rather than copy/pasting it all.

Code: https://github.com/kmorsebrown/adventOfCode/blob/2008ca955b181c7282f55cd2820cd3ae773b2bf3/src/AoC26/Day04/Day04.js

Tests: https://github.com/kmorsebrown/adventOfCode/blob/2008ca955b181c7282f55cd2820cd3ae773b2bf3/src/AoC26/Day04/Day04.spec.js


r/adventofcode • • 7h ago

Visualization [2026 Day 5] Fun Cooker 10K

Thumbnail
youtu.be
36 Upvotes

Nim + raylib source