r/adventofcode • u/daggerdragon • Dec 13 '17
SOLUTION MEGATHREAD -๐- 2017 Day 13 Solutions -๐-
--- Day 13: Packet Scanners ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hints^ยค?
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!
16
Upvotes
View all comments
Show parent comment
1
u/vash3r Dec 14 '17 edited Dec 14 '17
What's going on in the
residues =line:We have a current list of residues (delays) modulo our previous LCM. This starts with
LCM=1andresidues = [0], ie the delay must be a multiple of 1.Then, we find the new LCM, and find all residues modulo the new LCM that are congruent to one of our residues from the old LCM. For example, if we had a solution that had to be 2 mod 4 (
residues=[2],lcm=4), and our nextmis 6, then our solution must be one of[2, 2+4, 2+4+4]modulo LCM(4,6)=12. If residues was [2,4] mod 6 and our nextmwas 8, our solution would be in[2, 2+6=8, 2+6+6=14, 2+6+6+6=20, 4, 4+6=10, 4+6+6=16, 4+6+6+6=22]mod 24. The key here is that inrange(i,lcm,prev_lcm)we increment byprev_lcm, so each number in that range will also be congruent to i modprev_lcm.This step will generate
len(residues) * m / gnew possible residues.Finally, we filter out the new residues that aren't allowed with
if x%m not in neq[m]. In the first example, if it could not be 4 mod 6 (neq[6]=[4]), then we would get[2, 6]as our new residues mod 12 (10 was eliminated). In the second example, if it could not be 0 or 4 mod 8, then we would get[2, 14, 10, 22]mod 24.This step usually removes many of the possible residues. It seems like the new amount of residues is
len(residues)*(m/g - len(neq[m]))(where g is the gcd and alsom/g==lcm/prev_lcm), but I can't think of exactly how to prove it.