-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.go
62 lines (54 loc) · 1.07 KB
/
day25.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
i, err := ioutil.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay25Part1(intListToSlice(input)))
}
//SolveDay25Part1 returns the encryption key for the door and card
func SolveDay25Part1(input []int) (s int) {
if len(input) != 2 {
return 0
}
value, subjectNumber := 1, 7
var loopSize, keySubjectNumber int
for {
loopSize++
value *= subjectNumber
value %= 20201227
if value == input[0] {
keySubjectNumber = input[1]
break
}
if value == input[1] {
keySubjectNumber = input[0]
break
}
}
value = 1
for i := 0; i < loopSize; i++ {
value *= keySubjectNumber
value %= 20201227
}
return value
}
//intListToSlice converts the list of numbers (each number one row) to a slice
func intListToSlice(list string) (i []int) {
for _, line := range strings.Split(strings.TrimSuffix(list, "\n"), "\n") {
lineInt, err := strconv.Atoi(line)
if err != nil {
return nil
}
i = append(i, lineInt)
}
return
}