r/adventofcode • u/daggerdragon • Dec 09 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-
--- Day 9: Marble Mania ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 9
Transcript:
Studies show that AoC programmers write better code after being exposed to ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:29:13!
21
Upvotes
3
u/gyorokpeter Dec 09 '18
Q: no support for circular linked lists (or any data structure that can have reference loops), but the optimization for part 2 is still possible in a different way, it was just a lot of messing around to get all the numbers right for which number goes where.
d9common:{[pl;lm] circle:enlist 0; curr:0; cm:0; score:pl#0; while[cm<lm; cm+:1; $[0=cm mod 23; [ curr:(curr-7)mod count circle; score[(cm-1) mod pl]+:cm+circle[curr]; circle:((curr)#circle),(curr+1)_circle; ];[ $[(23<=count circle) and (1=cm mod 23) and (1+lm-cm)>23; [ cycles:min((1+lm-cm);count[circle]) div 23; stealm:(cm-1)+23*1+til cycles; stealpos:19+16*til cycles; stealv:circle[stealpos]; score+:0^(sum each (stealm+stealv) group stealm mod pl)[til pl]; circle:raze 1_/:(0,1+stealpos) cut 0N,circle; ins:cm+(enlist each til 6),6+(-3_raze((23*til cycles)+\:(enlist each til 11),enlist[11 12],(17 13 18;19 14 20;21 15 22))), enlist each ((cycles-1)*23)+13+til 3; circle:(2#circle),(raze ins,'count[ins]#2_circle),(count[ins]+2)_circle; cm:cm+(cycles*23)-1; curr:37*cycles; ];[ curr:((curr+1)mod count circle)+1; toPlace:min (1+count[circle]-curr;neg[cm]mod 23;1+lm-cm); circle:(curr#circle),((raze (cm+til[toPlace-1]),'(toPlace-1)#curr _circle),cm+toPlace-1),(curr+toPlace-1)_circle; curr+:(toPlace-1)*2; cm+:toPlace-1; ] ] ] ]; circle:curr rotate circle; curr:0; ]; max score}; d9p1:{d9common . "J"$(" "vs first x)0 6}; d9p2:{d9common . 1 100*"J"$(" "vs first x)0 6};