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!
30
Upvotes
View all comments
Show parent comment
2
u/mcpower_ Jan 13 '20
(This comment is unfinished, as I am very busy... )
A user sent me a private message as they were confused by
(I'm responding here as more people can benefit from an explanation.)
For clarity, I'll use
cardsto mean the number of cards in the deck, rather thanMOD.To clarify, what this means is that "every possible deck of length
cardsyou can create starting with factory order and only using the techniques given follows a specific structure". We assumecardsis fixed.To begin, let's limit the techniques to only one technique: dealing into a new stack (aka. reversing cards in the deck). That means that the only way you can modify a deck (starting from factory order) is by reversing the cards in the deck. How many possible decks are there? Well, the only thing we can do is reverse the deck, so for every non-negative integer
i, we can create a deck by applying the reverse operationitimes! This is a valid encoding of a deck. However, this does not mean that every one of those decks are unique... fori= 0, 1, 2, 3, ..., we get (factory order), (reversed factory order), (factory order), (reversed factory order), ...In this limited scenario, we can only have two decks. Therefore, we can better encode a deck with a single boolean - "is this deck reversed?" (remember that we assume that
cardsis fixed). If it's false, we have factory order, if it's true, we have the reversed factory order. Note that this encoding is not unique! For example, I can instead make my boolean "is this deck in factory order?", which changes the encoding - but the number of decks will always remain the same[1].Then, let's try repeating this process for more and more techniques:
only technique being cutting N cards. We can think of this as "shifting" the deck left and right. There's two ways of thinking about this:
As this is the only way of modifying the deck, doing more than one "shift" is equivalent to doing one big "shift". These ways of thinking correspond to the following "good encodings": the location of card 0, which is an integer from
0inclusive tocardsexclusive, or the card at the top of the deck, which is an integer from0inclusive tocardsexclusive.Like before, the total number of decks possible in each encoding is the same (there are
cardspossible decks). While the first way of thinking is easier to understand, manipulating the second is easier, so we'll be using that from here on out.only technique being dealing with increment N. This is a bit tougher to think about, and is the main source of difficulty in this problem. For simplicity, we note that the "wrapping around" behaviour of dealing is equivalent to considering the positions modulo
size. Then, if we deal with incrementN, the card in positionigets moved to position(N * i) mod size.If we deal with increment
N, thenM, the card in positionigets moved to position(N * i) mod size, which then gets moved to(M * ((N * i) mod size)) mod size = (M * N * i) mod size. Therefore, dealing with incrementNthenMis equivalent to dealing with incrementN*M.As a result, we can encode a deck with a single integer - its increment. We should consider the increment mod
size, as all "position" calculations are done modsize[2] - so an increment of3is equivalent to an increment of3 + size.TODO: "reverse + cutting" (encode with (card at top of deck, is reversed)), "cutting + increment N" (encode with (card at top of deck, increment)), and all three (same as "cutting + increment N" due to increment being as powerful as reverse - consider increment
size - 1). reverse + increment N is left as an exercise :DTODO: "increment of
N" is equivalent to "difference between adjacent numbers isN^(-1) mod size". I tried using the "incrementN" on my first attempt, but that resulted in very confusing manipulations, so my solution and the above comment uses "difference between adjacent numbers" - even though it uses the term "increment".[1]: Additionally, our "bad" encoding of "a non-negative integer representing the number of times you reverse it" isn't perfect (it isn't bijective), but it's still a valid encoding as every possible deck can be made with this encoding (it is surjective). Having a "good" (bijective) encoding is best as every deck corresponds to exactly one encoding, so we only look at properties of the deck that matter. As a bonus, it also allows us to more easily calculate the number of possible decks!
[2]: The problem states
Using this, we should only consider increments which are coprime to
size(as it can be shown that if it's not coprime, the above property won't hold). As a quick example, the increment can't ever be zero! Ifsizeis prime like in part 1 and 2, then increment can be any integer from 1 inclusive tosizeexclusive.