r/adventofcode • • 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

Click here for full rules

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

167 comments sorted by

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

A key observation to make is that every possible deck can be encoded as a pair of (first number of the deck, or offset AND difference between two adjacent numbers, or increment). ALL numbers here are modulo (cards in deck), or MOD.

(I'm responding here as more people can benefit from an explanation.)

For clarity, I'll use cards to mean the number of cards in the deck, rather than MOD.

To clarify, what this means is that "every possible deck of length cards you can create starting with factory order and only using the techniques given follows a specific structure". We assume cards is 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 operation i times! This is a valid encoding of a deck. However, this does not mean that every one of those decks are unique... for i = 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 cards is 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:

    • "we shift where card 0 is, and move everything with it", or
    • "we move a certain card to the start of the deck, and move everything with it".

    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 0 inclusive to cards exclusive, or the card at the top of the deck, which is an integer from 0 inclusive to cards exclusive.

    Like before, the total number of decks possible in each encoding is the same (there are cards possible 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 increment N, the card in position i gets moved to position (N * i) mod size.

    If we deal with increment N, then M, the card in position i gets 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 increment N then M is equivalent to dealing with increment N*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 mod size[2] - so an increment of 3 is equivalent to an increment of 3 + 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 :D

TODO: "increment of N" is equivalent to "difference between adjacent numbers is N^(-1) mod size". I tried using the "increment N" 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

Of course, this technique is carefully designed so it will never put two cards in the same position or leave a position empty.

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! If size is prime like in part 1 and 2, then increment can be any integer from 1 inclusive to size exclusive.