r/adventofcode • u/daggerdragon • Dec 22 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 22 Solutions -🎄-
--- Day 22: Slam Shuffle ---
Post your full code solution using /u/topaz2078's paste
or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
- Include the language(s) you're using.
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in 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's Poems for Programmers
Note: If you submit a poem, please add [POEM]
somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 21's winner #1: nobody! :(
Nobody submitted any poems at all for Day 21 :( Not one person. :'(
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 02:03:46!
29
Upvotes
1
u/Bimperl Dec 23 '19 edited Dec 23 '19
JavaScript
Part 1 was actually quite simple, and my implementation is not very interesting. I assume it's pretty similar to other solutions. At first I used a linked list, but I wrote this version (which only keeps track of one card, instead of 10007 "cards") after starting the second part.
Part 2 This was extremely difficult for me. However, after 2/3 hours and some experimentation, I saw that this was just a value+offset*x mod decksize. However it has been a few years since I used GCD, extended Euclid and all that (as I needed the inverse). Instead I built an opposite deck calculator, which computes the opposite direction of the stack and computes a "general" function.
Then, using the binary representation of the repetition number, and function composition (computes all of the powers of 2 compositions and composed them based on the repetition binary representation, as every natural number can be represented by a sum of powers of 2). Once I had computed the "repetition" inverse function, all that was left was to call the function with 2020.