-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
51 lines (40 loc) · 1.17 KB
/
Solution.cs
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
namespace AdventOfCode2020.Day25
{
internal class Solution
{
private readonly EncryptionSubject _card;
private readonly EncryptionSubject _door;
public Solution(long publicKey1, long publicKey2)
{
_card = new EncryptionSubject(publicKey1);
_door = new EncryptionSubject(publicKey2);
}
public long PartOne() => _card.Handshake(_door);
}
internal class EncryptionSubject
{
private readonly long _loopSize;
public EncryptionSubject(long publicKey)
{
PublicKey = publicKey;
_loopSize = 0;
var result = 1L;
while (result != PublicKey)
{
result = Transform(result, 7L);
_loopSize++;
}
}
public long PublicKey { get; }
public long Handshake(EncryptionSubject other)
{
var key = 1L;
for (var i = 0; i < _loopSize; i++)
{
key = Transform(key, other.PublicKey);
}
return key;
}
private static long Transform(long a, long b) => a * b % 20201227L;
}
}