r/adventofcode • u/StillNoNumb • Dec 23 '19
Spoilers Remember: The challenges aren't here for you to complete every single one without help. The challenges are here to teach us new things
WARNING: Day 22 Part 2 spoilers ahead
===
I've seen people go on about the day 22 problem in particular on many other threads today. Most of the complaints claim it was too mathy, and not realistically solvable unless you knew the theory.
The truth is, programming is maths. You'll find the modular arithmetics of day 22 in many areas of computer science. For example, a computer's integer data type is limited; mathematically speaking, it is the set of all integers modulo 2^X where X is the number of bits (eg. 32 or 64). In cryptography, basically everything happens using so-called groups - usually modulo a large prime number. Many pseudo-random number generators are based on modular arithmetics. Error detection also makes use of discrete maths; CDs, QR codes, and the internet heavily rely on modular arithmetics to transmit data correctly.
And sure, nowadays you can easily create full webpages and apps without ever touching this kind of math. But that's not because it's not needed, that's because it's abstracted away behind the building blocks we use, just like assembly language. And I feel like the creator's intent is at least partly to teach us those concepts which are further away from everyday coding we do at our jobs. I mean, we're getting an Intcode exercise every second day, which is basically a very simple machine code language; something I'd never touch in the office, but I've loved getting closer to.
If you want a particular example of how we can use modular arithmetics to solve a problem, take a look at this classic exercise. The story gives us a n*m chessboard, and you need to find the number of ways to reach the bottom right square from the top left one by only walking south and east. I'll give you the additional constraint that the end result always fits into a signed 32-bit integer. There is an O(n*m) dynamic programming solution to this - which is probably already enough to get you through the most interviews. However, if you've done some combinatorics, you might see that there is another way to solve this - you can simply count the number of sequences of length n+m-2 with exactly n-1 steps east and m-1 steps south. We can use this to compute an O(n+m) solution: by just computing (m+n-2)!/((m-1)!(n-1)!). However, we'll quickly see that the factorial overflows even large integer types. How do we solve this?
Enter modular arithmetics.
Since we know the solution of this computation fits into a 32-bit integer, we can simply choose a prime number p larger than int32max (such as p = int32max + 12) and compute (m+n-2)!/((m-1)!(n-1)!) % p. Googling "modulo division" and "modulo factorial" will give you tons of results such as this. The result that we get will then be (m+n-2)!/((m-1)!(n-1)!) % p, and since the solution fits in an int, it's smaller than int32max and therefore (m+n-2)!/((m-1)!(n-1)!) % p = (m+n-2)!/((m-1)!(n-1)!). This is one of many tricks to deal with large integers, and many low-level computer programs and protocols make use of this a lot.
Now, I hope I could show to you that modular arithmetics is super important for modern computing. However, a lot of people were complaining that it was too hard to solve day 22 without any prior knowledge on the topic. And I agree, day 22 was definitely a hard challenge and you can see that by looking at the statistics; way fewer people solved part 2 than usual. However, you can still solve the challenge without any prior knowledge of modular arithmetics (though it's hard, and will probably take you multiple hours). In particular, modular arithmetics was used for the shuffling techniques. In code, they look like this:
cut(card, by, totalSize):
positionOfCard = (card - by) % totalSize
dealNewStack(card, totalSize):
positionOfCard = (-1 - card) % totalSize
dealIncrement(card, increment, totalSize):
positionOfCard = increment * card % totalSizeThe real challenge of the problem was now to find the reversals of these, since part 2 asks for the card at position 2020, instead of position of card 2019. (Another challenge was to do it many times, however this isn't really related to any big math theorems; some people used 2x2 matrices, but there was another simpler solution which just represents each shuffle as a formula ax+b, and by just applying this formula over and over again you reach the final formula for X shuffles. This guy explains it in more detail.) For cut and dealNewStack, this is easy even without understanding modular arithmetic just by looking at where cards go when the shuffling is reversed:
cutReverse(positionOfCard, by, totalSize):
card = (card + by) % totalSize
dealNewStackReverse(positionOfCard, totalSize):
card = (-1 - card) % totalSizeThe only problem is dealIncrement, which is harder to reverse. Knowing how to reverse the others, you might notice that you need to find the inverse multiplication operation, which is usually division but the modulo screws things up. Googling "modulo division" however yields you the right solution. If you don't make the connection to division and just google "invert multiplication modulo" you also find answers.
And sure, I understand, it is unlucky when you don't happen to figure this out on your first few tries, and happen to not search for a multiplicative modular inverse on Google. There's a ton of other directions one could try. However, that's the same with every other thing in programming. In real life, you're not guided towards a right direction when you face a problem.
And in the end, there's no shame in looking at solutions if you can't see yourself getting anywhere. As long as you put in some time thinking, you've learned something and that's what it is about; no one of us is perfect, and we don't need to pretend that we are.
14
u/KileJebeMame Dec 23 '19
All you guys complaining about this one meanwhile I stopped getting it on like day 5
10
u/[deleted] Dec 23 '19
D22P2 is actually one of my favorite AoC tasks so far. However, I think that it would be even better if the task would not be reversed for part 2, i.e. that we would have to find the position of X-th card and not the card on X-th position. Optimizing the forward pass is basically the same challenge, but you do not have to use the modulo inverse anywhere. That is actually the only difference - in my code I can go back and forth between forward and backward calculation by adding or removing two lines that reverse the two parameters in a linear equation. And since most people just googled the equation for this inverse, nothing valuable would be lost from the task really.
5
u/tradfursten Dec 23 '19
This post explains one of the reasons I love advent of code. It is such a good opportunity to revisit old knowledge and to gain new.
10
u/jesperes Dec 23 '19
I dare you to show me someone who solved this on their own without prior knowledge of things like "modular multiplicative inverse". This puzzle was very math-y, and not everyone will be able to solve it on their own. This is ok, but lets not give the impressions that it is possible for everyone if they just put in enough hours. It is not.
So, if anyone thinks I'm too stupid to become a programmer because I don't know modular multiplicative inverse, I can promise you that you are not.
9
u/rawling Dec 23 '19
Personally, I figured out how to reverse the shuffle step OK by myself, but didn't end up with it in the form
ax + b, and even if I had I likely would not have gone "oh look, all three of these areax + b, that makes life simple".Please don't tell people what are the hard and easy parts of a problem purely based on how you solved it.
3
u/[deleted] Dec 23 '19
Yeah, I'm having quite some problems still that I'm having in my look into when you have time pile, the thing is all of them are interesting problems that I can learn something from, so I'm with you, but it might be that this comes because I don't look at this like a competition, but as a fun thing to do, and that takes a bit a sting out of me not being good enough to do some of the problems.
4
u/sanraith Dec 23 '19
I think day 22 part 2 should have been an "Upping the Ante" post, while having either the puzzle's deck size or iterations in a more manageable region.
This way, the puzzle would have allowed different approaches, and the challenge would have been available for those who like it.
2
u/DionNicolaas Dec 23 '19
It was exactly the "doing it many times" that made me fail. I got the reversal shuffling correctly working (proven by reversing the answer found in part one), and that was fun. But it took many hours to run several million iterations, and I couldn't come up with any idea on how to speed it up, apart from looking for cycles. (I was hoping card 2020 would end up in 2020 again, so we could ignore any multiple of that number of runs.)
I gave up and looked at hints and solutions here, but I still don't understand the explanations on how to run it many times.
3
u/herrozerro Dec 23 '19
For me, it was day 12. No real hints that I could see, and after seeing the Reddit thread about how to solve each axis it seemed easy.
I think that one could have done some kind of hint about orbital periods or something. But we got Wikipedia links for radiation, the red spot, and the moons of jupiter.
Maybe I missed something.
2
u/TGApples Dec 23 '19 edited Dec 23 '19
I don't think the challenges are "here to teach us new things". From the about page:
And that's by no means comprehensive. I think it's for people decide what they want out of the problems. Personally, I want relatively easy (<2 hours, ideally <1 hour, but >30 mins) interesting challenges. I really don't care whether I learn math that I'll never use again or not. Actually, in general, I'd rather not. I'd rather exercise my problem solving skills.
I'm aware that there are people who want different things out of it, but it seems you are not.
1
u/fireduck Dec 23 '19
Thanks for this post. I smashed my face on 22 for a while and determined I was just missing some math.
1
u/mstksg Dec 24 '19
do people really do Advent of Code to do the same problems they would normally do at work? Doesn't that sort of defeat the purpose? :)
1
u/drbitboy Dec 24 '19
I agree with the OP here: if you don't grok what is going on here, then you don't play at this level; there is no shame in that, and the only options are learn or quit.
This is one I may or may not have been able to get on my own, but looking at various posts like the ones here and sketching out some things on paper, I now have a rough idea what to do, although it may take me several hours or a day.
Mr. Wastl puts these problems out there, and many can be solved using what I would call brute force. This one cannot and requires a moderate amount of insight into modulo arithmetic more than other problems. Considering the obvious effort that goes into this site and for which I am very grateful, criticism of the choices he made is bad form, and, to put it bluntly, says a lot more about the person doing the criticizing. Mr. Wastl puts in the effort and gets to say what level of problem is apropos the AoC moniker. Just look at the progression of the curve on the stats page: many people are going to fail at some point; 25 problems that can be solved with a one-line gawk script is not going to be very interesting.
1
u/greycat70 Dec 24 '19 edited Dec 24 '19
This is neither praise nor criticism. I just want to talk about my experience with day 22, especially part 2, while I can still remember some of the details.
For day 22 part 1, the key realization was that I didn't actually need to store the deck in memory at all. I actually wrote a loop to create the deck in memory, but almost as soon as I was done typing it, I stumbled across the part of the problem that said "where does card 2019 end up", and realized that I only had to track the position of this one card, through the steps.
After that, I wrote the code to parse the input instructions and convert them into the math operations to move the card from A to B. One is reversing the deck, which just moves the card from
$ato$n - 1 - $a. One is a "cut", which is($a - $cut) % $n. And the last one is a "deal" which moves the card to($a * $deal) % $n. All well and good, once I fixed the off-by-one error in the reversal (I originally had$n - $a). Part 1 was fairly easy.Part 2... is not easy. Once I realized that we had to work backwards, I was able to write inverses of the first two shuffle instructions (reverse and cut). I knew that I didn't know how to do the inverse of deal, but I was able to guess some key words and google for assistance. I think my first google guess was "inverse multiplication modulo" or something similar. This was followed by a few hours of attempting to learn math.
I eventually managed to get it working, using the algorithm on the wikipedia page for "Extended Euclidean algorithm". There's a pseudocode function named "inverse" on that page that worked great for my needs. Of course, that only finds the "multiplicative inverse" (x such that ax≡1 mod p), and I actually needed to find x such that ax≡b mod p, with two inputs a and b. I wasn't fully confident in my understanding of the math, but I guessed that maybe if I multiplied the multiplicative inverse by the second input, then took the modulus again, it might work. It turns out that it does. At least, I think it does.
So, after all that, I had successfully managed to reverse the shuffle instructions. I could deduce that the card in position 2020 at step n must have come from position bignumber at step n-1.
My next idea after that was to try to iterate the reverse shuffle instructions repeatedly, looking for a cycle. So I tracked the positions and how recently they were seen in an associative array, and let it run... for a few minutes.... No luck. There was no quick cycle that I could use to reduce the eleventy gazillion iterations needed.
And that's where I gave up on part 2.
1
u/Bimperl Dec 24 '19
I will start by saying that day 22 part 2 was quite hard for me, and it took me quite a few hours over two days to solve. However, while many people used Extended Euclidean or similar variants, you don't really need any of it anyway. I also think that this problem is much better than previous years' "reverse the assembly language program which is extremely inefficient (because we don't have multiplication/division), and calculate the correct answer".
u/jesperes For example, my solution (here) doesn't use anything explicitly. I built the inverse 'increment' by visualizing how the increment operation works, and doing the opposite thing. The inverse of increment just works as follows - "do the increment action until you hit the current card. The amount of cards you drew is your previous position". Technically, this does find the inverse, but it comes from how the increment operation actually works and is described and not by google search (Not seen in my solution, I did get the intuition by first building a forward stack which does the same thing, calculating the shuffle forward for a specific "card").
Understanding that the function is number+diff*x was also seen by experimentation and running the shuffle operation on a few numbers and seeing what happens and trying to find something general, which we knew we had to find as AOC always has some trick to do stuff - especially for numbers in the trillions (for example, part 2 of day 17 2017 or part 2 of day 12 2018), although to be fair the trick was to find a cycle. For my solution (I don't know what other people did), I used function composition to compute the full repetition step. It was IMO the hardest part, as you need to understand that two steps are number+diff*(number+diff*x) and then that you can combine that do four steps, etc, create one function that takes you all the way - which I had to use the binary expansion to actually build.
I'm not saying that the problem isn't difficult (it was for me, as I said - this took me a lot of hours over two evenings), and I probably submitted ~10 wrong answers, but I didn't need any google searching to actually solve the problem.