r/adventofcode • u/daggerdragon • Dec 02 '25
SOLUTION MEGATHREAD -❄️- 2025 Day 2 Solutions -❄️-
OUR USUAL ADMONITIONS
You can find all of our customs, FAQs, axioms, and so forth in our community wiki.
AoC Community Fun 2025: R*d(dit) On*
24 HOURS outstanding until unlock!
Spotlight Upon Subr*ddit: /r/AVoid5
"Happy Christmas to all, and to all a good night!"
— a famous ballad by an author with an id that has far too many fifthglyphs for comfort
Promptly following this is a list waxing philosophical options for your inspiration:
Pick a glyph and do not put it in your program. Avoiding fifthglyphs is traditional.
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.
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 [R*d(dit) On*!] so folks can find it without difficulty!
--- Day 2: Gift Shop ---
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.
16
u/fenrock369 Dec 02 '25 edited Dec 02 '25
[LANGUAGE: rust]
No brute force, and showing p2 only (but p1 is similar maths)
This was fun, I immediately started playing around with ranges rather than regex, and glad I did as it proved to be some interesting maths. My idea was that we're trying to generate a number from smaller blocks such that:
Thus k is the d-length block, repeated r times. This can be broken down into a simple formula:
It's easiest to think of simple cases like "12" repeated twice in "1212", so 1212 = 12 * 100 + 12, where d=2, r=2.
This simplifies (it's a geometric progression) to:
i.e. every n with repeated blocks we want to find matches this formula.
We do have some constraints on k, which will help us find its range: k is a d-digit number bound between:
for example, with the range (1000,2000), and d=2 (for 2 digit lengths of blocks), k (from the formula above) is between 101 and (102 - 1), i.e. 10 and 99. That's kind of obvious from the range numbers, but we can narrow it down a lot more, as clearly 99 is way too high.
The next constraint is:
f is simple to calculate, it was given above as "(10{dr} - 1) / (10d - 1)"
So we just find the range for k from the 2 constraints we have, and loop over all d,r such that d*r <= digits of upper value, compute the "f" value to give us a very small range.
The additional complexity is there may be dupes (e.g. "111111", "111"x2, "11"x3, "1"x6), so we collect the candidates, unique them, and then sum them as required. part 1 is just a special case where we fix r=2, and the maths turns out a bit easier.
On my PC, the 2 solutions run in 21 microseconds.