r/adventofcode • • Dec 03 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 3 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's secret ingredient is… *whips off cloth covering and gestures grandly*

Spam!

Someone reported the ALLEZ CUISINE! submissions megathread as spam so I said to myself: "What a delectable idea for today's secret ingredient!"

A reminder from Dr. Hattori: be careful when cooking spam because the fat content can be very high. We wouldn't want a fire in the kitchen, after all!

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 3: Gear Ratios ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:11:37, megathread unlocked!

110 Upvotes

1.3k comments sorted by

View all comments

2

u/duckyluuk Dec 03 '23 edited Dec 03 '23

[LANGUAGE: Python Golf]

Input Parsing:

*I,=open("3.txt")

Part 1:

print(sum(int(l[j:k])
    for i,l in enumerate(I) 
    for j in range(len(l)-1)
    for k in range(j,j+4)
    if(1-l[j-1:k].isdigit())*l[j:k].isdigit()*(k>=len(l)or 1-l[k].isdigit())*
      any(c not in"1234567890.\n"for x in I[max(0,i-1):i+2]for c in x[max(0,j-1):k+1])
))

Part 2:

print(sum(len(q:=[int(m[x:k])
      for m in I[max(0,i-1):i+2]
      for x in range(max(0,j-3),j+2) 
      for k in range(max(x,j),x+4)
      if((k>=len(m))or 1-m[k].isdigit())*m[x:k].isdigit()*((x<1)or 1-m[x-1].isdigit())
     ])==2and q[0]*q[1]
    for i,l in enumerate(I)
    for j in range(len(l)-1)
    if"*"==l[j]
))

Stats:

Input Parsing: 11 bytes

Part 1: 244 bytes | 0.152 seconds

Part 2: 271 bytes | 0.005 seconds

Total: 526 bytes

Today was pretty tough to golf down, but in the end I managed to keep both parts a one-liner.
The way I'm looking for numbers in part 1 is very slow (see how much longer it takes than part 2) but it works and it's somewhat small so that's good enough. Might try to improve more later but I'm pretty happy with the sizes for now!

1

u/[deleted] Dec 03 '23

[removed] — view removed comment

1

u/azzal07 Dec 03 '23

You could shave few characters from the gear multiplication condition by padding the lists with zero:

P[c]=P.get(c,[])+[int(m[0])]
P[c]=[int(m[0])]+P.get(c,[0])  # padding adds 1 character

print(sum(map(sum,t:=P.values())),sum(p[0]*p[1]for p in t if len(p)>1))
print(sum(map(sum,t:=P.values())),sum(p[0]*p[1]for p in t)) # saves more than 1

Am I correct, that your solution assumes only the gears * to have two numbers adjacent? I ended up checking explicitly for * in my solution.

2

u/Professional-Top8329 Dec 03 '23

we're down to 241 bytes already.

*I,=open(a:=0);R=range;D=str.isdigit;b=sum((q:=[a-(a:=a+int(s))for m in I[i-1:i+2]for x in R(j+2)for k in R(x+4)if~-D(m[k:k+1])*~-D(m[x-1])*D(s:=m[x:k])*i>0<j<=k]+[0])[0]*q[1]for i in R(140)for j in R(140)if I[i][j]in"*#%@&-=+/$");print(a,b)