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

1

u/Jyrroe Dec 23 '19 edited Dec 23 '19

I must be misunderstanding something, perhaps someone can help. I understand the concept of modular multiplicative inverse (I think), but can't wrap my head around how to calculate it. Let's skip the explanation of the "deal with increment" technique for a moment and just look at the calculation:

increment *= pow(n, MOD-2, MOD)

Now let's grab one of the examples from Day 22 Part 2, a 10-card deck and "deal with increment 7":

0 3 6 9 2 5 8 1 4 7

Representing this deck with an offset and increment, the initial values were (0, 1), and are now(0, 3) correct? So in this case I want to know the modular multiplicative inverse of 7 mod 10, i.e. n = 7 and MOD = 10. If I try to reach this conclusion with the above calculation I get:

increment = increment * pow(n, MOD-2, MOD)
increment = 1 * pow(7, 10-2, 10)
increment = 7^8 mod 10
increment = 5764801 mod 10
increment = 1

What am I doing wrong here?

EDIT: I think I understand now, this "shortcut" calculation only works when MOD is prime, and so doesn't apply to this simple 10-card deck example. It does however apply to an 11-card deck:

0 8 5 2 10 7 4 1 9 6 3

And the calculation:

increment = increment * pow(n, MOD-2, MOD)
increment = 1 * pow(7, 11-2, 11)
increment = 7^9 mod 11
increment = 40353607 mod 11
increment = 8

2

u/mcpower_ Dec 23 '19

EDIT: I think I understand now, this "shortcut" calculation only works when MOD is prime, and so doesn't apply to this simple 10-card deck example.

Yep - this specific calculation only works when MOD is prime. To be exact, this uses Fermat's little theorem which only works when it's prime.

You can extend this to work with non-prime (composite) MOD with Euler's theorem, which generalises Fermat's little theorem. However, you'll need to calculate the prime factorisation of MOD to use it. With 10 = 2**1 * 5**1, this would become:

inv(n) = pow(n, (2-1)*(5-1) - 1, 10) = pow(n, 3, 10)

The other method is to use the extended Euclidean algorithm - if you're using Python, this is built into Python as of 3.8 with pow(n, -1, MOD) or you can use the implementation that Raymond Hettinger posted in the BPO discussion about the feature.

1

u/Jyrroe Dec 23 '19

Thanks for the details! I was able to use the extended Euclidean algorithm to solve some examples by hand, but struggling to translate that into code.