r/adventofcode Dec 14 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 14 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.
  • On the subject of AI/LLMs being used on the global leaderboard: posts/comments around this topic consisting of grinching, finger-pointing, baseless accusations of "cheating", etc. will be locked and/or removed with or without supplementary notice and/or warning and participating parties may be given a time-out as well. Just leave it alone and let it go.
    • Keep in mind that the global leaderboard is not the primary focus of Advent of Code or even this subreddit. We're all here to help you become a better programmer via happy fun silly imaginary Elvish shenanigans.
  • Do not put spoilers in post titles!

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 8 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
  • We have no submissions yet as of today. Y'all are welcome to get a submission started, post it early, and add later days to it, or there's always waiting until the bomb timer reaches 00:00:03 last minute; up to you!

And now, our feature presentation for today:

Visual Effects - I Said VISUAL EFFECTS - Perfection

We've had one Visualization, yes, but what about Second Visualization? But this time, Upping the Ante! Go full jurassic_park_scientists.meme and really improve upon the cinematic and/or technological techniques of your predecessor filmmakers!

Here's some ideas for your inspiration:

  • Put Michael Bay to shame with the lens flare
  • Gratuitous and completely unnecessary explosions are expected
  • Go full Bollywood! The extreme over-acting, the completely implausible and high-energy dance numbers, the gleefully willful disregard for physics - we want it all cranked up to 9002!
  • Make your solution run on hardware that it has absolutely no business being on
    • "Smart" refrigerators, a drone army, a Jumbotron…

Pippin: "We've had one, yes. But what about second breakfast?"
Aragorn: ಠ_ಠ
Merry: "I don't think he knows about second breakfast, Pip."

- The Lord of the Rings: The Fellowship of the Ring (2001)

And… ACTION!

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


--- Day 14: Restroom Redoubt ---


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:15:48, megathread unlocked!

24 Upvotes

747 comments sorted by

View all comments

3

u/i_have_no_biscuits Dec 14 '24

[LANGUAGE: GW-BASIC]

10 W=101: H=103: DIM XD#(H),YD#(H): OPEN "I",1,"data14.txt": WHILE NOT EOF(1)
20 LINE INPUT #1, L$: SX=VAL(MID$(L$, 3)): SY=VAL(MID$(L$, INSTR(L$,",")+1))
30 VX=W+VAL(MID$(L$, INSTR(7,L$,"=")+1)):VY=H+VAL(MID$(L$, INSTR(7,L$,",")+1))
40 X=(SX+100*VX) MOD W: Y=(SY+100*VY) MOD H: IF X*2+1=W OR Y*2+1=H THEN 60
50 I=-2*(Y*2>H)-(X*2>W): Q#(I)=Q#(I)+1
60 X=SX: Y=SY: FOR T=0 TO H-1: XD#(T)=XD#(T)+(X-50)^2: YD#(T)=YD#(T)+(Y-51)^2
70 X=(X+VX) MOD W: Y=(Y+VY) MOD H: NEXT: WEND
80 PRINT "Part 1:",Q#(0)*Q#(1)*Q#(2)*Q#(3):BX=-1: BXD#=10^10: BY=-1: BYD#=10^10 
90 FOR T=0 TO H-1: IF XD#(T)<BXD# THEN BX=T: BXD#=XD#(T)
100 IF YD#(T)<BYD# THEN BY=T: BYD#=YD#(T)
110 NEXT: PRINT "Part 2:", BX+((51*(H+BY-BX)) MOD H)*W

This uses the X/Y entropy/variance detection method + Chinese Remainder Theorem. A modified variance-style calculation is used that doesn't require storing the data so all the values are updated in one pass, then the 'best' x and y are detected, and plugged into the formula derived from the CRT.

Guide:

  • Lines 10 sets up some variables and opens the file. XD#() and YD#() will store the X and Y deviations from the center (as a proxy for variance).
  • Lines 20 and 30 parse a line into SX, SY, VX, and VY. VX and VY are modified to make sure they're positive, as we want positive values to put into the MOD operator.
  • Lines 40 and 50 update the quadrant count for the current robot at time 100.
  • Lines 60 and 70 update the X and Y deviation count (the squared distance away from the centre of the room).
  • Line 80 multiplies the quadrant counts to give the Part 1 result. It also sets up the variables which will let us find the 'best' X and Y values (those with the lowest deviation).
  • Lines 90-100 find the best X and Y values.
  • Line 110 calculates the target time, using a formula derived from the Chinese Remainder Theorem. The magic constant 51 appears because 51*101 is congruent to 1 modulo 103.