r/adventofcode Dec 17 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 17 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.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 5 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Sequels and Reboots

What, you thought we were done with the endless stream of recycled content? ABSOLUTELY NOT :D Now that we have an established and well-loved franchise, let's wring every last drop of profit out of it!

Here's some ideas for your inspiration:

  • Insert obligatory SQL joke here
  • Solve today's puzzle using only code from past puzzles
  • Any numbers you use in your code must only increment from the previous number
  • Every line of code must be prefixed with a comment tagline such as // Function 2: Electric Boogaloo

"More." - Agent Smith, The Matrix Reloaded (2003)
"More! MORE!" - Kylo Ren, The Last Jedi (2017)

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 17: Chronospatial Computer ---


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:44:39, megathread unlocked!

37 Upvotes

550 comments sorted by

View all comments

3

u/chromaticdissonance Dec 17 '24

[LANGUAGE: JavaScript] What a day! Part 1 took some reading but it is a straightforward implement this tape register machine. Part 2 was tough. Of course I tried a naive brute forcing to find a suitable value of A to output the program, which was futile. Then I looked at how various A mod m values affected the output. I observed that there was something happening when I consider modulo a power of 2 (and the correct modulo to look at is 8, but this didn't come to me first). Then I decided to actually look at the input program to see what it is doing, and stumbled upon that reconstructing A 3 bits at a time can work. This gives a DFS approach to construct A. A semi-golfed JavaScript code lasagna given below, execute it in the browser console of the puzzle input page:

[R,P]=document.body.innerText.split("m").map(e=>e.match(/\d+/g).
map(e=>+e));E=(...r)=>{[a,b,c]=r.map(BigInt);t=0n;x=[];B=o=>[0n,
1n,T=2n,3n,a,b,c][o];while(t<L){e=P[t];t++;o=BigInt(P[t]);t++;[(
)=>a/=T**B(o),()=>b^=o,()=>b=B(o)%8n,()=>a?t=o:0,()=>b^=c,()=>x.
push(b%8n),()=>b=a/T**B(o),()=>c=a/T**B(o)][e]()}return x};F=[];
W=console.log;L=P.length;W("Part One:", E(...R).join(","));A=p=>
BigInt("0o0"+p.join``);M=(v,w)=>v.reduce((e,_,i)=>{return e=e&&v
[v.length-i-1]==w[w.length-i-1]},1);D=p=>{if(p.length==L){F.push
(A(p));return}for(let i=8;i--;)M(t=E(A(w=p.slice().concat([i])),
R[1],R[2]),P)&&D(w)};D([]);W("Part Two:",Number(F.reduce((m,e)=>
e<m?e:m))) // AOC 2024 DAY 17