Skip to content

Commit ad4ad67

Browse files
committed
2020 Day 25
1 parent bdecac5 commit ad4ad67

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

2020/25a.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from math import ceil, sqrt
2+
3+
4+
# Baby-Step-Giant-Step implementation by 0xTowel
5+
# https://gist.github.com/0xTowel/b4e7233fc86d8bb49698e4f1318a5a73
6+
def bsgs(g, h, p):
7+
'''
8+
Solve for x in h = g^x mod p given a prime p.
9+
If p is not prime, you shouldn't use BSGS anyway.
10+
'''
11+
N = ceil(sqrt(p - 1)) # phi(p) is p-1 if p is prime
12+
13+
# Store hashmap of g^{1...m} (mod p). Baby step.
14+
tbl = {pow(g, i, p): i for i in range(N)}
15+
16+
# Precompute via Fermat's Little Theorem
17+
c = pow(g, N * (p - 2), p)
18+
19+
# Search for an equivalence in the table. Giant step.
20+
for j in range(N):
21+
y = (h * pow(c, j, p)) % p
22+
if y in tbl:
23+
return j * N + tbl[y]
24+
25+
# Solution not found
26+
return None
27+
28+
29+
def main():
30+
mod = 20201227
31+
card_pub = 3418282
32+
door_pub = 8719412
33+
34+
card_loop = bsgs(7, card_pub, mod)
35+
door_loop = bsgs(7, door_pub, mod)
36+
print(card_loop)
37+
print(door_loop)
38+
39+
# Check if the computation was correct
40+
print(pow(7, card_loop, mod))
41+
print(pow(7, door_loop, mod))
42+
43+
# This is the answer
44+
print(pow(door_pub, card_loop, mod))
45+
print(pow(card_pub, door_loop, mod))
46+
47+
48+
main()

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
my answers.
44
* Personal Stats 2020
55
| Day | Time | Rank | Score | Time | Rank | Score |
6-
| 25 | | | | | | |
6+
| 25 | 00:32:11 | 2354 | 0 | 00:32:44 | 1968 | 0 |
77
| 24 | 00:23:36 | 1362 | 0 | 01:21:19 | 2287 | 0 |
88
| 23 | 00:53:56 | 2265 | 0 | 02:09:18 | 1579 | 0 |
99
| 22 | 00:17:33 | 2337 | 0 | 01:50:06 | 2972 | 0 |

0 commit comments

Comments
 (0)