dekulan

Advent of Code 2020 day 25, part 1

Dec 2nd, 2021 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.19 KB | None | 0 0
  1. # Number Theory ...
  2. #
  3. # The puzzle seems to be based on a form of cryptographic accumulator
  4. #
  5. # There are two numbers, a and b, and the final key that is is generated
  6. # is of the form
  7. #
  8. #    7 ** ab mod 20201227
  9. #
  10. # The "public keys" are 7**a and 7**b
  11. #
  12. # Actually, the way the puzzle states it, the final key is
  13. #  (7 ** a) ** b, but we can replace this with 7**ab
  14.  
  15. To solve, first find a and b by looking up power % prime tables:
  16.  
  17. $ perl -E 'my $x = 1; for my $n (1..20201227) { $x = $x * 7 % 20201227; say "$n: $x" }' | egrep " 10705932| 12301431"
  18. 529361: 12301431
  19. 2232839: 10705932
  20.  
  21. This shows that a is 529361 and b is 2232839
  22.  
  23. Next, we have to find 7 ** (529361 * 2232839). However, this is too large to
  24. calculate directly, so using a result from number theory, we replace this with:
  25.  
  26. 7 ** ((529361 * 2232839) % (20201227 - 1)
  27.  
  28. Perl is able to calculate this without resorting to bignums:
  29.  
  30. $ perl -E '$ans = ((529361 * 2232839) % 20201226); say $ans'
  31. 4152619
  32.  
  33. Finally, look up that value in the left-hand column of the original power % prime script:
  34.  
  35. $ perl -E 'my $x = 1; for my $n (1..20201227) { $x = $x * 7 % 20201227; say "$n: $x" }' | egrep '^4152619'
  36. 4152619: 11328376
  37.  
Add Comment
Please, Sign In to add comment