-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (69 loc) · 1.44 KB
/
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
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
80
81
82
83
package main
import (
"fmt"
"os"
"strconv"
"github.com/sanderploegsma/advent-of-code/2019/go/utils"
)
func main() {
input, err := utils.ReadFile("input.txt")
if err != nil {
fmt.Printf("failed to read input: %v", err)
os.Exit(1)
}
digits := make([]int, len(input))
for i := range input {
digits[i], _ = strconv.Atoi(string(input[i]))
}
fmt.Println(PartOne(digits))
fmt.Println(PartTwo(digits))
}
func PartOne(digits []int) []int {
res := FFT(digits, 100)
return res[:8]
}
func PartTwo(digits []int) []int {
signal := make([]int, len(digits)*10000)
for i := 0; i < 1000; i++ {
for j := 0; j < len(digits); j++ {
signal[i*j] = digits[j]
}
}
offsetStr := ""
for i := 0; i < 7; i++ {
offsetStr += strconv.Itoa(digits[i])
}
offset, _ := strconv.Atoi(offsetStr)
res := FFT(signal, 100)
return res[offset+1 : offset+9]
}
func FFT(input []int, repeat int) []int {
res := make([]int, len(input))
copy(res, input)
patterns := make(map[int][]int)
for r := 0; r < repeat; r++ {
temp := make([]int, len(input))
for i := range input {
p, ok := patterns[i+1]
if !ok {
p = pattern(i+1, len(input))
patterns[i+1] = p
}
v := 0
for j := range input {
v += res[j] * p[j]
}
temp[i] = utils.Abs(v) % 10
}
res = temp
}
return res
}
func pattern(repeat, length int) []int {
p := []int{0, 1, 0, -1}
res := make([]int, length+1)
for i := range res {
res[i] = p[i/repeat%len(p)]
}
return res[1:]
}