r/adventofcode • • 22h ago

Visualization [2026 Day 4] Barcode Scanning Visualization

68 Upvotes

23 comments sorted by

13

u/raman4183 22h ago

I still can’t come up with anything or understand the logic to solve this.

12

u/Boojum 21h ago

Basically, for a given barcode:

  1. Look at all the letters to see if they all match each other, ignoring the ? wildcards.
  2. Now check the even letters to see if they all match each other, and the odds to see if they all match.
  3. Now check every third letter beginning on the first to see if they all match, every third letter beginning with the second, and every third letter beginning on the third.
  4. Now check every fourth letter...

Stop at the first case where each of the subsets contains only matching letters or wildcards. If nothing else, you can terminate when every letter is in its own group, because then each group (of one) trivially has everything within it matching.

4

u/miserableMolvi 21h ago

thank you for this, I was unable to pick the pattern from visualization.

3

u/TheRealRory 17h ago

You saved me. I had no idea what to do.

2

u/Zestyclose-Remove550 18h ago

Tysm!!! this helped me get in the correction direction. lol was messing with kmp and lps table before this.

2

u/TheRealRory 15h ago

I just noticed your username, does it have anything to do with the Burrito chain in Ireland?

2

u/Sea_Cut_6635 12h ago

This is the entire reason i was able to solve it so easily. Part 1 & part 2.

1

u/nik282000 1h ago

Ahh, I knew modulus would be in this one somewhere. I just used it in the worst way possible.

3

u/vegan_antitheist 18h ago

This animation is literally the solution. But you don't need to create a table like this and check every column like that.
It's even easier if you just create an empty chunk (initially all just ???...) and iterate the chunks from the barcode to check if they fit or not. For each code in the chunk, if your extra chunk has a ? you just copy the new code. If it already has a code and your current chunk has a different one you must increase the chunk size and retry.

5

u/TheRealRory 17h ago

The problem is its really difficult to tell what the animation is doing if you are clueless, like me. Like I couldn't understand what each column was representing. The movement seemed completely random to me. OP's follow up comment really helped

1

u/raman4183 17h ago edited 17h ago

I think this is basically what gemini recommended to me. I did understand how it works but i will probably end up forgetting how it works after a couple of days maybe.

I found yesterday’s problem a lot more easier.

1

u/vegan_antitheist 17h ago

It's not really that useful information. I never do anything like this as a programmer. Coding challenges are fun. You can rarely use this knowledge anywhere else. But it trains your brain to think out of the box and being able to do that can help you in all kinds of situations. That's why you really shouldn't use an AI here. You won't actually train your brain to find the simple solution.

It can sometimes be useful for security when you are trained to see solutions that aren't obvious. Lots of people just can't understand that the hacker doesn't care about their password when they can just circumvent the login and access your secret data directly or by hijacking their session somehow.

3

u/Boojum 22h ago

I thought that taking the barcodes elements and moving them around to rewrap them into successively larger groups, combined with showing where we get consistent repeats would make for an interesting visualization. So here it is. Not a super tricky puzzle, but it did take me a few minutes just to parse the description of it for what I needed to do.

This is showing Part 1. But in many ways, I'd say that Part 2 is even simpler; the exact same process from Part 1 works, except that there's no outer loop and totaling up (just concatenating the lines into one big string).


Made in Python with a small custom framework.

Complete self-contained source for this animation.

2

u/imp0ppable 19h ago

This is great but might be even better to show it tackling the example inputs?

2

u/imp0ppable 18h ago

If this further helps anyone, this is why rgbrgbrgbr comes out to 3:

r  r  r  r
 g  g  g  
  b  b  b
   r  r  r
    g  g
     b  b
      r  r
       g
        b

that's taking every 3rd character of the barcode with the starting position moving up one each time. Every group you get is homogenous, including the two single character ones at the end (they always are but that doesn't matter)

3

u/vegan_antitheist 17h ago

I don't understand this. Where would you even see that it's 3?

moving up one each time

What does that mean? Where is "up"? What is "each time"?

1

u/imp0ppable 15h ago

Where would you even see that it's 3?

It's every third character of rgbrgbrgbr. You try every character first, nope, then try every other character, nope not homoegenous, then try every third character - check the groups you get and that's the answer because they're all homogenous.

What does that mean? Where is "up"?

Sorry I should have said rightward not up.

What is "each time"?

You're going to need an outer loop for pattern length and an inner loop for start position. So "each time" in that inner loop the place you start iterating from is moved right by one.

Does that help?

1

u/vegan_antitheist 14h ago

It's every third character of rgbrgbrgbr. 

That would just be an "bbb".
I still don't get how you do it.
A common solution is to split the barcode to chunks. You try chunks of length, 1, then 2, then 3 etc. Then you just check if all chunks are compatible by ignoring ? but comparing all other characters. I don't know if you do the same.

1

u/imp0ppable 14h ago

A common solution is to split the barcode to chunks. You try chunks of length, 1, then 2, then 3 etc

I mean yes that is what I'm saying but maybe not explaining it well.

That would just be an "bbb"

Well it would be r r r r, then g g g, then b, b, b - which is what I was trying to show. I think just taking the first group, r r r r, doesn't prove the pattern holds so you have to check the others all match too.

Have you solved it btw? Mine worked for both parts right away but I could probably speed it up if i'm missing something?

1

u/vegan_antitheist 14h ago

Mine works but my first one was brute force and probably would take years to solve part 2. I haven't done coding challenges in 10 years.

1

u/imp0ppable 14h ago

Yeah mine is outer loop for pattern length and an inner loop for start position.

Mine is kind of slow at about 30s, it's O (n2) which is sad. I could definitely speed it up but if you're just trying to get the answer, that method does work.

It is essentially the same as the visualisation here but just wanted to share my mental model of it.

1

u/vegan_antitheist 8h ago

30 is extreme. I just create a new array for each length (you could optimise this) and initialise it with ? (fill with ?). Then I check each chunk to see it it still fits. It tales less than a second.

2

u/rmarianoa 17h ago

Thank you! This visualization was a great hint for me to come up with an algorithm! :)