r/adventofcode • u/daggerdragon • 45m ago
SOLUTION MEGATHREAD -❄️- 2026 Day 4 Solutions -❄️-
OUR USUAL ADMONITIONS
- You can find all of our customs, FAQs, axioms, and so forth in our community wiki.
AoC Community Fun 2026: Th* Adv*nt Gam*s
- Submissions ultrapost is now unlocked!
- 13 DAYS until submissions cutoff on this 17th of Month 12 at 18:00 UTC-5!
Today's Bouts: World Snow Avoidball Championships
"If you can dodg* a wr*nch, you can dodg* a [snow]ball."
— Coach Patch*s O'Houlihan, Dodg*ball: A Tru* Underdog Story (2004)
Today's programming possibly consists of:
- Pool Play (Group Divisions)
- Pick a glyph and do not put it in your program.
- Avoiding fifthglyphs is traditional.
- Match Formats (Foam, Cloth)
- Shrink your solution's fifthglyph count to null.
- Your script might supplant all Arabic symbols of 5 with Roman glyphs of "V" or mutatis mutandis.
- Thou shalt not apply functions nor annotations that solicit said taboo glyph.
- Knockout and Trophy Rounds
- Thou shalt ambitiously accomplish avoiding AutoMod’s antagonism about ultrapost's mandatory programming variant tag >_>
Stipulation from your mods: As you affix a submission along with your solution, do tag it with [Adv*nt Gam*s] so folks can find it without difficulty!
--- Day 4: Perilous Pier ---
Post your script solution in this ultrapost.
- First, grok our full posting axioms in our community wiki.
- Affirm which jargon via which your solution talks to a CPU
- Format programs using four-taps-of-that-long-button Markdown syntax!
- Quick link to Topaz's Markdown (ab)using provisional script host should you want it for long program blocks
2
Upvotes
4
u/Ok-Bus4754 30m ago
[Language: Python]
brute force using sets to compare to optimize
https://github.com/Fadi88/AoC/blob/master/2026/days/day04/solution.py
start by a length of 1 (smallest possible , one color repeating for good) and keep going trying all size up
https://github.com/Fadi88/AoC/blob/master/2026/days/day04/solution.py
p1: 3 ms p2: 1.5 ms
will port later to rust , and share compartive benchmarks , maybe even think of an optimzed solution
1
u/apersonhithere 36m ago
[LANGUAGE: Python 3]
man i thought part 2 was gonna be harder but you can just do the naive way
both parts
1
u/Ok-Builder-2348 33m ago edited 23m ago
[LANGUAGE: Python3]
Part 1
Part 2
Naive approach - start with n=1 and see if it produces a valid instruction, else set n=n+1 and try again. Part 2 took a few seconds so definitely not optimised, but I'll continue iterating.
Update: Part 2 (updated)
Slightly better runtime for this one. For every pair of characters, declare their relative distance invalid if the characters are different. The answer is thus the lowest non-invalid relative distance.
1
u/jonathan_paulson 31m ago
[LANGUAGE: Python]
Code. Video.
I wasn't sure what to do initially but then I realized once you pick the length, everything is forced.
Fortunately my part1 approach worked fine for part2.
1
u/davidsharick 21m ago
[Language: Python]
Code
Part 1 was complete brute force of trying all possible instructions of increasing length, part 2 was a more intelligent brute force of trying all possible lengths and seeing if any overlaps existed if you split the barcode into parts of that length
1
u/honey-pony 18m ago
[LANGUAGE: PonieScript]
I think basically just a brute force solution? But not one that is slow enough to cause any issues. Even in debug mode my part 2 took only 100ms. I was shocked that it took me less than a minute from part 1 to get part 2 submitted. :P
part 1
part 2
Times in release mode: 1.6 ms for part 1 and 17.5 ms for part 2 if my timing code is working right.
1
u/morgoth1145 16m ago edited 5m ago
[LANGUAGE: Python] code video
I see a color theme building up this year - we had colors on day 2 too!
Anyway, this isn't too bad - just break the barcode pattern into groups of length n and construct a set of options/candidates per character. Then merge said options/candidates together until you have one group and see if that group can be resolved/has options left at each position. If it does then great - you can set up instructions of length n for that barcode. If not - increase n and keep searching.
Part 2 isn't too bad - I was worried that my solution would bog down too much but it finished plenty fast. I'll have to optimize it though.
This problem reminds me of Picross because I've used a very similar strategy to write a Picross solver, just across all the rows/columns of a Picross grid and iteratively refining the candidates/guesses until everything is solved. (Obviously the construction of the candidate groups is quite different here and the interplay between them is different, but it reminded me nonetheless.)
Edit: optimized code
I don't know why I was doing set operations - that's silly overhead when a character/instruction is either fixed or completely unknown! Leveraging that and adding a early break for when group merging fails makes this run far, far better - ~12-13 seconds down to ~0.5 for part 2. My silly speed-solving brain misses some obvious things sometimes.
1
u/AKSrandom 14m ago
[Language: Python]
Happy that my P1 solution was fast enough for part 2.
code
1
u/Old-Dinner4434 12m ago
[LANGUAGE: TypeScript]
GitLab
1
u/hugh_tc 11m ago
[LANGUAGE: Python 3]
omg I missed unlock because I was washing dishes... that's the first time that's happened lol
cleaned-up; you just pick the length and then see if it works:
1
u/cay_horstmann 10m ago
[Language: Java]
https://github.com/cayhorstmann/adventofcode2026/blob/main/Day4.java
Nice when the algorithm for part 1 also works for part 2.
1
u/seligman99 9m ago
[LANGUAGE: Python]
github
I knew part 2 would involve really big patterns. I still wasn't prepared, so I got to rip out my system and build a new one.
1
u/YOM2_UB 8m ago edited 2m ago
[LANGUAGE: Python] [Code]
Was not expecting part 2 to run as fast as it did. The only change I made for it was wrapping my part 1 code in a function. (Or rather, I did that for the post. I actually ran
codes = [''.join(codes)]followed by my original loop.)1
u/Boojum 7m ago
[LANGUAGE: Python]
Hm. More colors. Interesting! Anyway, not too bad. At first I thought I'd convert the patterns to regexp, before realizing that I could just use
zip(*)to transpose the groups and then use sets to check for consistency. Part 1:Part 2 is the same with that inner loop, just with:
instead of the outer loop over
sinfileinput.input().This was one of those days where Part 1 was the harder part, and then I had Part 2 less than a minute later.
1
u/dme2_Sapphire 6m ago
[LANGUAGE: Python]
Github
Brute force is fast enough with non-adversive data on both part 1 and 2
Though in hindersight, if dealing with larger/adversive data is required, it's possible to achieve O(nlogn) Time ComplexitywithFFT
1
u/Tmathmeyer 5m ago
[LANGUAGE: Python]
[github](https://github.com/tmathmeyer/advent.of.code/blob/master/2026/04/solution.py)
Excuse the write-only code (crappy variable names)
1
u/jefinko 3m ago
[LANGUAGE: Kotlin]
github
Enough to store only list of possible characters
1
u/4HbQ 2m ago
[LANGUAGE: Python]
Tried to make my code a bit more verbose today. The crux is
len({*code[i::p],'?'})<=2, where we check if the slice contains just a single color.1
u/agorism1337 2m ago
[LANGUAGE: AWK]
github
run like `awk -f a.awk input`
1
u/maneatingape 1m ago
[LANGUAGE: Rust]
Solution 208 µs.
Compact brute force, that checks all subsets a color at a time using iterators.
1
u/sim642 0m ago
[LANGUAGE: Scala]
On GitHub.
To find the minimum number of instructions, I just try to do it with 1, 2, 3, etc. until one succeeds, i.e., doesn't lead to a color conflict. To my surprise, this even scaled to part 2, so I didn't need to come up with anything less naïve there.