-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
41 lines (35 loc) · 953 Bytes
/
main.go
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
package main
import (
"fmt"
input "aoc2020/inpututils"
)
func main() {
fmt.Println("--- Part One ---")
fmt.Println(Part1("input.txt"))
}
func Part1(filename string) int {
publicKeys := input.ReadNumbers(filename)
publicKey1, publicKey2 := publicKeys[0], publicKeys[1]
loopSize1, loopSize2 := getLoopSize(publicKey1), getLoopSize(publicKey2)
encryptionKey1 := transform(publicKey2, loopSize1)
encryptionKey2 := transform(publicKey1, loopSize2)
if encryptionKey1 != encryptionKey2 {
panic(fmt.Sprintf("%v != %v", encryptionKey1, encryptionKey2))
}
return transform(publicKey1, loopSize2)
}
func getLoopSize(publicKey int) int {
subject, val, size := 7, 1, 0
for val != publicKey {
size += 1
val = (val * subject) % 20201227
}
return size
}
func transform(publicKey int, loopsSize int) int {
subject, value := publicKey, 1
for loops := 0; loops < loopsSize; loops++ {
value = (value * subject) % 20201227
}
return value
}