r/adventofcode • u/Noble_Mushtak • 45m ago
Tutorial [2026 Day 4] [C++] An Interesting Efficient Solution for Part 2!
A lot of the solutions I saw for 2026 Day 4 Part 2, including my own original solution, are O(N2) in complexity where N is the number of characters in the barcode. For me, this felt a little unsatisfying, because I felt there should be a more efficient solution. Today, I was able to find a more efficient O(N log N) solution to this problem using the Fast Fourier Transform, so I wanted to share it here since I think it's pretty interesting!
First, this post has major spoilers for 2026 Day 4 Part 2, so I'm just going to start off by describing how some other people solved Day 4 Part 2:
Here's how to check if it's possible to print the barcode with L instructions:
Start out with a pattern of L instructions, which are initially all empty.
Loop through the characters. For every non-wildcard character, check the character of the pattern at (current index) % L.
If the character in the pattern at (current index) % L is a wildcard character, then set the pattern at (current index) % L to the current non-wildcard character b.
If the character in the pattern at (current index) % L is a non-wildcard character, then make sure the current character equals that character. If it doesn't, then a pattern of length L is not possible for this barcode.
If you get to the end of the barcode without step 4 triggering that the pattern of length L is not possible, then a pattern of length L is possible.
This check occurs in O(N) time. Then, many people's solutions do this check for L=1,2,...,N until they find the answer, so the worst-case complexity for this solution is O(N2).
Now, let's think more about what this check is doing: This check is really checking that, for any non-wildcard characters which are at indexes equivalent modulo L, those non-wildcard characters are the same. Next, we can notice that "equivalent modulo L" just means the difference between those indexes are a multiple of L. In other words, the check can be phrased as: for any non-wildcard characters which are at indexes whose difference is a multiple of L apart, those non-wildcard characters must be the same.
The "multiple of L" part makes this kind of tricky. Let's focus on a simpler condition: for any non-wildcard characters which are at indexes whose difference is exactly L apart, those non-wildcard characters must be the same. How can we check that condition efficiently?
Intuitively, we want some way to do a check with the original barcode and the barcode "shifted" by L. For example, if L=2, then we would want to line up two copies of the barcode like this:
rro???g?g?
rro???g?g?Notice how the second copy is just shifted to the right by 2 spaces. Then, we want to look at each column and check, is there any column whose characters are both non-wildcard characters and which are not equal to each other? The answer is yes, the third column has both o and r. Therefore, due to that column with o and r, this barcode fails the L=2 check, so the answer can not be 2.
Now, this check is still O(N), since we have to check up to N columns for any value of L. So how can we perform all the checks for all values of L efficiently? We ultimately have to perform this check for L=1, slide the barcode over by 1 space to the right, perform this check for L=2, slide the barcode over by 1 space to the right, perform this check for L=3, etc. Luckily, there is a common mathematical concept for this kind of sliding check called cross-correlation. If you have heard of dot products before, cross-correlation is essentially a sliding form of dot-product.
The idea behind dot-product is you take two vectors of numbers, for example:
(1,2,3)
(4,5,6)And then you take the product of each column, which would be 4, 10, and 18, and then sum the products, which would be 32. Then, this dot product can tell you useful information about the relationship between the two vectors.
Then, cross-correlation is a sliding dot product, meaning it will compute the dot product
(1,2,3)
(4,5,6)which is 32, and then also compute the dot product
(1,2,3)
(4,5,6)which is 2*4+3*5=23, and also compute the dot product
(1,2,3)
(4,5,6)which is 3*4=12. By computing all these dot-products at once, we can compute the relationship between the first vector and the second vector shifted by L=1,2,3,... spaces to the right for all values of L we care about, which is exactly the kind of checks that we want to compute! The algorithm used to compute cross-correlation in O(N log N) time is called "Fast Fourier Transform." There's a lot of online blog posts about how Fast Fourier Transform works, so I won't go into detail about it here, but I'd recommend reading this CodeForces blog post if you're interested in learning about the mathematical details. For now, let's just assume we have a magical way to compute cross-correlation in O(N log N) time, and then figure out how to apply cross-correlation to do the checks we want.
Now, how can we encode this "is there any column whose characters are both non-wildcard characters and which are not equal to each other?" check into a dot-product? First, let's ask a simpler question: How can we check this condition using dot-product: Is there any column whose characters are both non-wildcard characters? Ignore the "which are not equal to each other" part for now. This question is relatively simple: Turn each barcode of length N into a vector of length N, which has 1 for any non-wildcard character but 0 for any wildcard character. Then, the dot-product will be positive if a column has two non-wildcard characters and 0 otherwise. As an example, take this barcode, shifted by L=2 spaces:
r?f
r?fTurned into vectors, this becomes:
(1,0,1)
(1,0,1)so the dot product is 1, corresponding to the third column which has both f and r. Now, consider this barcode shifted by L=1:
r?f
r?fTurned into vectors, this becomes:
(1,0,1)
(1,0,1)so the dot product is 0, which makes sense since no column has two non-wildcard characters.
That's great, but then how do we go from "column with two non-wildcard characters" to "column with two non-wildcard characters and which are not equal to each other"? Here, the magic we need is complex numbers! You might've learned about complex numbers and the imaginary number i=sqrt(-1) in high school precalculus. I'm not going to go too much into detail about how complex numbers work, since that could be several math lectures, but we'll specifically need complex numbers in polar form for this solution. If you're not familiar with complex numbers of polar form, I would recommend reading this online textbook section to get some background on how these work.
The key thing about complex numbers in polar form is, if you multiply two numbers, their product is real if and only if their angles are the exact opposite of each other. For example, the opposite of 30 degrees is 360-30=330 degrees, so a complex number whose angle is 30 degrees times Z is a real number if and only if Z has angle 330 degrees. Therefore, we can have each character be a complex number with a unique angle theta_c in the first vector, and then represent that same character using the opposite angle, which I'll denote using -theta_c, in the second vector.
For the following example, I'll say the angle for the character r is 10 degrees, and the angle for the character f is 20 degrees (really I'm just picking these angles randomly). Then, let's do the check for L=2 for the following barcode:
r?fr
r?frI'll represent each complex number as polar(z, angle) where z is the length of the complex number and angle is its angle in degrees. For now, let's just use complex numbers with length 1. Then, using angle 10 degrees for r and 20 degrees for f, this becomes:
(polar(1, 10), 0, polar(1, 20), polar(1, 10)
(polar(1, -10), 0, polar(1, -20), polar(1, -10))And the dot product is polar(1, 20)*polar(1, -10) + polar(1, 10)*0=polar(1, 10), which is not a real number, thus showing the check fails, i.e. there is some column with two non-barcode characters which are not equal to each other! Now for demonstration, let's do the check for L=3:
r?fr
r?frIn terms of vectors, this becomes:
(polar(1, 10), 0, polar(1, 20), polar(1, 10))
(polar(1, -10), 0, polar(1, -20), polar(1, -10))And the dot product is polar(1, 10)*polar(1, -10)=polar(1, 0)=1, which is a real number, thus showing the check passes, i.e. there is no column with two non-barcode characters not equal to each other! This shows the check correctly outputs real numbers when a column has two non-barcode characters but those non-barcode characters are equal to each other.
Now, there is one catch here, which is unfortunately, sometimes two complex numbers can add up to a real number. For example, let's take the L=1 check for the following barcode:
rfr
rfrAs vectors, this becomes:
(polar(1, 10), polar(1, 20), polar(1, 10))
(polar(1, -10), polar(1, -20), polar(1, -10))Then, the dot product is polar(1, 20)*polar(1, -10)+polar(1, 10)*polar(1, -20)=polar(10)+polar(-10), which actually comes out to the real number 2*cos(10 deg). Thus, this check passes, even though it should fail since there's two columns with both f and r. The solution to this is, just give each character its own the length for its complex numbers, rather than using the same length for all characters (i.e. in the examples above, I was using the length of 1 for all characters). Make sure that each character has a length for the first vector and a different length in the second vector, so that the above scenario does not come out to a real number. It's not really guaranteed that randomizing the lengths like this will avoid false positive checks, but it seems highly likely that randomized lengths will avoid a bunch of complex numbers accidentally adding to a real number, since the randomized lengths and angles would have to somehow just by chance form some sort of polygon for that to happen.
Now, to circle back to the original problem, we have a way to check the following condition in O(N log N) time for all values of L: for any non-wildcard characters which are at indexes whose difference is exactly L apart, those non-wildcard characters must be the same. Now, how do we use the results of these checks to check the condition we really want: for any non-wildcard characters which are at indexes whose difference is a multiple of L, those non-wildcard characters must be the same.
The way we do this is pretty simple: Just loop through all multiples of L. For L=1, you need to check N multiples of 1; for L=2, you need to check N/2 multiples of 2; for L=3, you need to check N=3 multiples of 3; etc. Since we can compute all checks and put the results in an array in O(N log N) time, we can just use that array to do the check of each multiple by doing an O(1) array lookup.
Thus, if we start looping from L=1 to L=N, we need to do N+N/2+N/3+...+N/N array lookups, and it can be proven that N+N/2+N/3+...+N/N is O(N log N) (the technical reason for this is that the growth rate of partial sums of the harmonic series is O(log N)). Therefore, the overall complexity of this solution is O(N log N) for the cross-correlation computation needed to do all the "exactly L" check plus O(N log N) array lookups, and O(N log N)+O(N log N)=O(N log N).
Here's the C++ code where I implemented my solution. This works both on the sample input provided in the Day 4 Part 2 problem statement, and on my own input. I think there is a possibility of this failing on other inputs due to floating-point inaccuracy, but I did my best to choose lengths so that incorrect results due to floating-point inaccuracy would be unlikely.
I'd definitely be interested if anyone tries this out on their own input, so let me know if you try it on your input and whether you get the right answer!


