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/Zv0n Dec 22 '19

Hi, I just wanna ask - how can we be sure that the geometric series formula will work when using modulo?

I was trying it out on small inputs and for example

(1 - pow(4,18,20))/(1-4) works as it should (returns 5)

but

(1 - pow(4,18,23))/(1-4) returns 2.33333333 when it should return 10

2

u/mcpower_ Dec 22 '19

When working under numbers modulo N, "division" in the normal definition doesn't make sense.

Consider N=11. Then 3*7 = 21, so 3*7 = 10. Intuitively speaking, that means 7 = 10/3 = 1.333333...... wait, that doesn't make any sense!

Instead, to "divide" a number, we require modular inverses. The modular inverse of x is some number a such that a*x = 1. In this case, 3*4 = 12, so 3*4 = 1 - 3 and 4 are modular inverses of each other, modulo 11.

We previously made the "intuitive" statement that 7 = 10/3. This isn't completely right, as we can't exactly divide by numbers mod N, but we can instead multiply by the inverse of the divisor. What's 10*3^(-1)? That's 10*4 = 40 = 7 which checks out!

Therefore, while 10/3 doesn't "exist" in numbers modulo 11, 10*3^(-1) = 7 works in the same way as 10/3 does in the rational numbers, and are basically equivalent modulo 11. We can apply the same logic to the geometric series formula.

Note, however, that this doesn't always work. Consider 3*5 modulo 12. Then 3*5 = 15 = 3, so 5 = 3*3^(-1) = 1??????? It turns out that 3 doesn't have an inverse modulo 12, so doing this is akin to dividing by zero. Only numbers which are coprime to N - i.e. gcd(x, N) = 1 - have inverses.

There's a few ways of calculating inverses. The method I used uses exponentiation, which is easier to implement as you probably need to use exponentiation somewhere else in this problem. The main other way of doing it is to extend the algorithm of calculating GCDs to give you an equation which you can rearrange to find the inverses.

1

u/bluegaspode Dec 22 '19

u/mcpower_ A big thanks, your post made me finally understand, why people start using modular inverse (and what it actually is).

1

u/naclmolecule Dec 22 '19

((1 - pow(4,18,20)) * mod_inverse(-3, 20)) % 20 division should be a mod inverse in this case

the second line should be ((1 - pow(4,18,23)) * mod_inverse(-3, 23)) % 23 which returns 10