-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.rb
41 lines (30 loc) · 865 Bytes
/
day25.rb
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
#file = File.open("input/day25.txt", "r")
#lines = file.readlines
$div_num = 20201227
# sample input
# card_public_key = 5764801
# door_public_key = 17807724
# actual input
card_public_key = 10705932
door_public_key = 12301431
def transform(subject_number, public_key, max_rounds = 0)
tmp = 1
rounds = 0
while true
rounds += 1
tmp *= subject_number
tmp = tmp % $div_num
if tmp == public_key
# puts "Card loop size: #{rounds}"
return rounds, tmp
end
if max_rounds > 0 and rounds >= max_rounds
return rounds, tmp
end
end
end
card_loop_size, _ = transform(7, card_public_key)
door_loop_size, _ = transform(7, door_public_key)
puts card_loop_size, door_loop_size
_, encryption_key = transform(door_public_key, nil, card_loop_size)
puts encryption_key