-
Notifications
You must be signed in to change notification settings - Fork 0
/
d25p1.py
52 lines (40 loc) · 1.12 KB
/
d25p1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import time
from utils.test_case import TestCase
from d25_input import INPUT
TEST_CASES = [
TestCase("""
5764801
17807724
""", (8, 11, 14897079)),
]
def find_loop(key, subject):
value = 1
loop_size = 0
while value != key:
value = value * subject
value %= 20201227
loop_size += 1
return loop_size
def apply_loop(loop_size, subject):
value = 1
for i in range(loop_size):
value = value * subject
value %= 20201227
return value
def solve(input):
card_key, door_key = [int(key) for key in input.strip().split('\n')]
card_loop_size = find_loop(card_key, 7)
door_loop_size = find_loop(door_key, 7)
if card_loop_size < door_loop_size:
encryption_key = apply_loop(card_loop_size, door_key)
else:
encryption_key = apply_loop(door_loop_size, card_key)
return card_loop_size, door_loop_size, encryption_key
if __name__ == '__main__':
for case in TEST_CASES:
result = solve(case.case)
case.check(result)
t0 = time.time()
print(solve(INPUT))
t1 = time.time()
print(f"{(t1 - t0) * 1000:0.1f} ms")