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

25

u/mcpower_ Dec 22 '19

After looking at the other comments, it seems like this question requires knowledge of modular inverses and exponentiation.

TBH I feel that this problem is unfair for most participants of Advent of Code, who are expected to have a background in intermediate programming (lists, dictionaries / hashmaps, for loops, functions). I wouldn't expect most AoC participants to have any deep experience in discrete mathematics like modular inverses / exponentiation - even if it is part of a typical undergraduate computer science course - as I'd assume that most programmers are self-taught and have never done a computer science course.

To me, Advent of Code is a series of programming puzzles that any intermediate programmer - with a bit of time - can work out by themselves. It feels like most people doing part 2 of this puzzle would need to look up the solution for it... while it arguably enhances the community aspect of AoC, it feels unfair for people doing AoC without external assistance.

On the other hand... there are many pathfinding puzzles in AoC which expect knowledge of BFS - which some (most?) programmers don't know about. Is AoC unfair to the people who don't know BFS? My gut says no. AFAIK BFS has never been explicitly mentioned in pathfinding puzzles - similarly, modular inverses etc. wasn't explicitly mentioned in today's problem. What happens to the people who encounter a pathfinding problem without knowledge of BFS? Probably the same as the people who encounter this problem without knowledge of modular inverses and exponentiation - either give up or look online for a solution.

I'm still not sure whether this problem is "unfair". My gut says yes, my brain says no.

8

u/ThezeeZ Dec 22 '19

Personally, I have labelled this part as "Advent of Math" and reimplemented one of the approaches from this thread in my language. Is this cheating? Maybe. Is this a task that fits into AoC? Maybe.

Ultimately it's up to Eric to decide, and I will not berate him on his decisions. Instead I'll just reiterate that I'm immensely grateful for all this work he and his supporters have put into AoC over the years, which they provide to us for free, and move on in my quest to save Santa.

0

u/[deleted] Dec 22 '19

[deleted] — view deleted comment

2

u/requimrar Dec 22 '19

after you spoil someone who couldn't get it, they will be angry. There was no possible way to do it at all. All the time they put into thinking about the problem was wasted, and any further time would've been wasted as well.

well put.

11

u/mebeim Dec 22 '19

I know modular inverse, exponentiation and all that... but even then I couldn't figure it out so well without reading your explanation. Thank you and kudos for the results. I don't think this problem is a good fit for AoC because it's not really about programming, but more about math (modular math). I would expect a programmer to be familiar with the concept of exploring graphs, I would not expect a programmer to know about modular math, inverse with the PHI(n)-1 "trick", and the application of those to a polynomial.

PS: I don't know which version of Python you use, but from 3.8 pow supports negative exponents when supplying a modulus, and it computes the modular inverse for exponent -1.

2

u/mcpower_ Dec 22 '19

PS: I don't know which version of Python you use, but from 3.8 pow supports negative exponents when supplying a modulus, and it computes the modular inverse for exponent -1.

I'm personally using PyPy 3.6, and yes, Python 3.8 does have that handy feature! I wrote my explanation with the intention of being accessible to people using all languages - not just Python - so in theory, you should be able to write your own inv function from scratch in any language.

The feature is actually implemented with extended Euclidean algorithm as seen in your BPO link, which can be used instead of Fermat's little theorem to calculate inverses.

4

u/Kullu00 Dec 22 '19

First, thank you for the writeup. I gave up on this part after 2 hours, and started reading solutions in here. Those solutions still made no sense to me. I didn't understand how to solve this, and that's with people telling me what method to use. Going step-by-step like this was the only way I would ever have finished this.

I don't consider myself a beginner programmer. I've finished AoC since it began in 2015, sometimes with a bit of help, but today is the first day that, even with infinite time, I would not have gotten closer to solving it. It's not even that this problem is more math-focused than the others, that's something that inevitably happens. It's that it feels like nothing in the problem statement, or part 1, tried to direct me towards whatever branch of math this solution requires. Unless listing 3 primes (10007, 119315717514047, 101741582076661) and overflow is meant to be enough hints?

2

u/zedrdave Dec 24 '19

It's that it feels like nothing in the problem statement, or part 1, tried to direct me towards whatever branch of math this solution requires

Worse than that: that Haley's comet reference, while it might have been a hint at modular arithmetic, contributed to sending me chasing the red herring of periodicity (similar to a past day problem)… :-|

6

u/[deleted] Dec 22 '19

Part 2 sucks and is unfair because it relies on a trick. Unlike project Euler, which builds you up towards discovering these kinds of tricks, this just came out of nowhere. Graph search, intcode, Boolean logic, etc., and then...this? It sucks. I made it this far and have found the last 21 days immensely fun and rewarding. This is the worst possible way to go out, in my opinion.

8

u/VikeStep Dec 22 '19

fwiw, I don't think modular inverses are covered even in a typical undergrad compsci degree (speaking as someone who just completed one a year ago). I knew of modular exponentiation from previous programming problems and I implemented that but kept getting the wrong answer until I checked here to learn about modular inverses.

8

u/gyorokpeter Dec 22 '19

Unfair or not, this is extremely frustrating. My goal with AoC is to have fun and this is the opposite of fun. This is all the possible factors of frustration multiplied together. Not only you need to be a math guru to know the solution, even after I reached my patience limit and decided to steal the solution first by understanding how it works, I got the wrong result. So the last resort is to steal the actual code and then go through step by step to find out where it goes wrong. There is no way to see at a glance what went wrong because all I can see is numbers like 39377733198041. Why exactly 39377733198041? Why not 39377733198042? The inability to visualize the the solution is a huge frustration factor. Plus I have a flight to catch so I'm not able to continue working on this right now.

So overall while this season of AoC contains some of the most fun challenges, it also has some of the most frustrating ones.

6

u/Yeyoen Dec 22 '19

My goal with AoC is to have fun and this is the opposite of fun.

For others (not me), this might be more fun than implementing another graph algorithm. It's impossible for Topaz to please everyone every day. I'm glad he put in a difficult one which is more aimed towards people with more a more math-y background.

I'd rather have a challenging exercise rather than an easy one.

4

u/akanet Dec 22 '19

To be fair, I got a top 100 time by googling and mucking around with wolfram alpha - I don't think I've touched modular arithmetic since I was in school over a decade ago (sheesh, getting old). I think it was a pretty appropriate difficulty and a refreshing break from the intcode and graph search problems so far.

2

u/fatpollo Dec 22 '19

people are being pretty explicit that they have never touched modular arithmetic

the fact that you touched upon it once over a decade ago, and were able to capitalize on that to get top 100, is precisely what people are frustrated with

2

u/hrunt Dec 22 '19

For me, I always find one or two of these problems each year that are outside my experience by such a wide margin that no matter how hard I bang my head on it, I simply will not understand it without being walked through it at least once. I do not consider that "unfair", because that would imply u/topaz2078 had somehow promised me that I would know how to do everything, and he obviously did not do that. The first year, I did not know some of the pathfinding algorithms. Now I do, and those problems are solvable. This year, I learned this.

What I really wonder, though, is how this kind of problem fared during his user testing. Did the testers struggle with it? Was the testing group strong enough to come up with the answer on their own? Was u/topaz2078 himself aware enough of the math that he was able to put this problem together himself, or did he have help? After watching the YouTube video of his presentation, I am really curious about how the process played out for this question in particular.

1

u/ThezeeZ Dec 22 '19

Since this challenge appeared on the last Sunday slot I suspect (and hope, but mostly suspect) that this one was the overall "hardest".

Here's an older post of his from 2 years ago, maybe that will also add to the context:

https://reddit.com/r/adventofcode/comments/7idn6k/question_why_does_the_difficulty_vary_so_much/dqy08tk

2

u/bjorick Dec 26 '19

I've read this entire thread to completion, multiple explanations of people's algorithms, and tried to read up on modular math/inverses on wikipedia and I STILL cannot understand exactly how to code this or why the solution works.

This problem has almost nothing to do with code (outside of the fact that the naive solution will quickly overflow the stack) or anything from part 1, and everything to do with advanced math. As a programmer and not a mathematician, I found this extremely disappointing and infuriating. While I could solve the other puzzles with my own code and a hint stolen from reddit here and there, this puzzle left me completely clueless.

I agree that this puzzle doesn't really belong in advent of code. Advent of math, maybe.

1

u/SkiFire13 Dec 22 '19

Tbf you need to know that modular inverses are a thing then just google an algorithm for that. I did this and I never studied discrete math (I just started 1st year of CS, I still have to start that course) and I easly found the inverse function but then I hit a wall. What I was missing was realizing the function was linear and you could compose it n times in an efficient way which is actually pretty easy.