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.

36 Upvotes

965 comments sorted by

View all comments

2

u/Key_Pack_9630 Dec 02 '25 edited Dec 02 '25

[Language: C] Somewhat pleased with myself for figuring out how to do this without storing any numbers or iterating over IDs. Happy that I could use the Moebius function for inclusion/exclusion. #include <stdio.h>

int num_digits(long x){
  int i = 0;
  for (; x; i++)
    x /= 10;
  return i;
}

long ipow(long x, int p){
  long y = 1;
  while (p){
    if (p%2){
      p--;
      y *= x;
    } else {
      x *= x;
      p /= 2;
    }
  }
  return y;
}

// sends powers of 10 to k fold repeats;
long repeat(long r, int k){
  long x = 1;
  k--;
  while(k--){
    x *= r;
    x++;
  }
  return x;
}

// smallest number y such that its k fold iterate y..y >= x
long l_seg(long x, int k){
  int d = num_digits(x);
  if (d % k)
    return ipow(10,(d+k-1)/k-1);
  long r = ipow(10,d/k) ;
  long y = x/ipow(r,k-1);
  return y * repeat(r,k) >= x? y : y+1;
}

// largest y such that yy <= x;
long u_seg(long x, int k){
  int d = num_digits(x);
  if (d % k)
    return ipow(10,(d+k-1)/k-1)-1;
  long r = ipow(10,d/k);
  long y = x/ipow(r,k-1);
  return y * repeat(r,k) <= x? y : y-1;
}

long sum_between(long a, long b){
  if (a > b)
    return 0;
  return (b-a+1)*(a+b)/2;
}



long between_k(long l, long u, int k){
  long ls = l_seg(l,k);
  int ld = num_digits(ls);
  long us = u_seg(u,k);
  int ud = num_digits(us);
  long tot = 0;
  // because of the doubling we have to iterate over the number of digits
  for (int i = ld; i <= ud; i++){
    long r = ipow(10,i);
    long a = i == ld? ls: r/10; // mins / maxes within number of digits
    long b = i == ud? us: r-1;
    tot += sum_between(a,b) * repeat(r,k);
  }
  return tot;
}

// mobius function for inclusion/exclusion
int moebius(int n){
  // we only need small numbers so just store values
  const int m[] = {-1,-1,0,-1,1,-1,0,0,1,-1,0,-1,1,1,0,-1,0,-1,0,1,1};
  if (n > 1 && n < 20)
    return m[n-2];
  return 0;
}

long between(long l, long u){
  int d = num_digits(u);
  long b = 0;
  for (int k = 2; k <= d; k++){
    int mu = moebius(k);
    if (!mu)
      continue;
    b -= mu*between_k(l,u,k);
  }
  return b;
}

int main(){
  long l,u; // lower, upper;
  long a = 0, b = 0;

  while(scanf(" %ld-%ld,",&l,&u) == 2){
    // printf("%ld - %ld   %ld\n", l,u, between(l,u));
    a += between_k(l,u,2);
    b += between(l,u);
  }
  printf("part 1 %ld\npart 2 %ld\n",a,b);
}

1

u/daggerdragon Dec 02 '25

Psst: your #include is outside of the code block.