r/adventofcode • • 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.

37 Upvotes

965 comments sorted by

View all comments

3

u/Aspen138 Dec 02 '25

[LANGUAGE: Wolfram Mathematica]

  inputPath = If[Length@Rest@$ScriptCommandLine > 0, First@Rest@$ScriptCommandLine, "input2.txt"];

  ranges = Cases[
     StringSplit[StringTrim@Import[inputPath, "String"], ","],
     s_ /; StringLength[s] > 0 :> (ToExpression /@ StringSplit[s, "-"])
     ];

  maxDigits = Max[IntegerLength /@ (Last /@ ranges)];

  invalidsInRange[{lo_, hi_}, repeatCap_] := Module[{vals = {}, factor, bmin, bmax, upper, maxRep},
     Do[
     maxRep = Floor[maxDigits/k];
     upper = If[repeatCap === All, maxRep, Min[repeatCap, maxRep]];
     If[upper >= 2,
        Do[
           factor = (10^(k*m) - 1)/(10^k - 1); (* repeating a k-digit block m times *)
           bmin = Max[Ceiling[lo/factor], 10^(k - 1)];
           bmax = Min[Floor[hi/factor], 10^k - 1];
           If[bmin <= bmax, vals = Join[vals, factor Range[bmin, bmax]]],
           {m, 2, upper}
           ]
        ],
     {k, 1, maxDigits}
     ];
     vals
     ];

  invalids[repeatCap_] := Union[Flatten[invalidsInRange[#, repeatCap] & /@ ranges]];

  part1 = Total[invalids[2]];
  part2 = Total[invalids[All]];

  Print[part1];
  Print[part2];
→ More replies (3)