-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.go
79 lines (65 loc) · 1.95 KB
/
day20.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
_ "embed"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
//go:embed input/day20
var d20 string
func day20(input string) (int64, int64) {
lines := strings.Split(input, "\n")
var numbers []int64
var numbersWithFactor []int64
var indexes []int
factor := int64(811589153)
for i := 0; i < len(lines); i++ {
if lines[i] == "" {
continue
}
num, _ := strconv.ParseInt(lines[i], 10, 64)
numbers = append(numbers, num)
numbersWithFactor = append(numbersWithFactor, num*factor)
indexes = append(indexes, i)
}
copyIndexes := make([]int, len(indexes))
copy(copyIndexes, indexes)
return d20p1(numbers, indexes), d20p2(numbersWithFactor, copyIndexes)
}
func d20p1(numbers []int64, indexes []int) int64 {
l := int64(len(indexes) - 1)
moveNumbers(numbers, indexes, l)
zero := slices.Index(indexes, slices.Index(numbers, 0))
length := len(numbers)
v1 := numbers[indexes[modularIndex((zero+1000)%length, length)]]
v2 := numbers[indexes[modularIndex((zero+2000)%length, length)]]
v3 := numbers[indexes[modularIndex((zero+3000)%length, length)]]
return v1 + v2 + v3
}
func d20p2(numbers []int64, indexes []int) int64 {
l := int64(len(indexes) - 1)
for n := 0; n < 10; n++ {
indexes = moveNumbers(numbers, indexes, l)
}
zero1 := slices.Index(indexes, slices.Index(numbers, 0))
v1 := numbers[indexes[modularIndex((zero1+1000)%len(numbers), len(numbers))]]
v2 := numbers[indexes[modularIndex((zero1+2000)%len(numbers), len(numbers))]]
v3 := numbers[indexes[modularIndex((zero1+3000)%len(numbers), len(numbers))]]
return v1 + v2 + v3
}
func moveNumbers(numbers []int64, indexes []int, l int64) []int {
for i := range numbers {
j := slices.Index(indexes, i)
indexes = slices.Delete(indexes, j, j+1)
newPosition := int((int64(j) + numbers[i]) % l)
idx := modularIndex(newPosition, int(l))
indexes = slices.Insert(indexes, idx, i)
}
return indexes
}
func modularIndex(i, mod int) int {
if i < 0 {
return i + mod
}
return i
}