-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (88 loc) · 2.04 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"github.com/Alex-Waring/AoC/utils"
)
type CheatSheat struct {
diffs [2000]int
values [2000]int
totals map[string]int
}
func part1(input []string) {
defer utils.Timer("part1")()
total := 0
for _, line := range input {
initial := utils.IntegerOf(line)
for i := 0; i < 2000; i++ {
initial = mutate(initial)
}
total += initial
}
fmt.Println(total)
}
func part2(input []string) {
defer utils.Timer("part2")()
cheat_sheats := []CheatSheat{}
for _, line := range input {
initial := utils.IntegerOf(line)
cheat_sheat := CheatSheat{
totals: map[string]int{},
}
for i := 0; i < 2000; i++ {
new := mutate(initial)
cheat_sheat.diffs[i] = onesDigit(new) - onesDigit(initial)
cheat_sheat.values[i] = onesDigit(new)
initial = new
}
cheat_sheats = append(cheat_sheats, cheat_sheat)
}
for _, cheat_sheat := range cheat_sheats {
for m := 3; m < 2000; m++ {
c_i, c_j, c_k, c_l := cheat_sheat.diffs[m-3], cheat_sheat.diffs[m-2], cheat_sheat.diffs[m-1], cheat_sheat.diffs[m]
key := fmt.Sprintf("%d,%d,%d,%d", c_i, c_j, c_k, c_l)
if _, ok := cheat_sheat.totals[key]; !ok {
cheat_sheat.totals[key] = cheat_sheat.values[m]
}
}
}
result := 0
for i := -9; i < 10; i++ {
fmt.Println(i)
for j := -9; j < 10; j++ {
for k := -9; k < 10; k++ {
for l := -9; l < 10; l++ {
total := 0
for _, cheat_sheat := range cheat_sheats {
if val, ok := cheat_sheat.totals[fmt.Sprintf("%d,%d,%d,%d", i, j, k, l)]; ok {
total += val
}
}
if total > result {
result = total
}
}
}
}
}
fmt.Println(result)
}
var mutateCache map[int]int
func onesDigit(n int) int {
return n % 10
}
func mutate(n int) int {
if val, ok := mutateCache[n]; ok {
return val
}
step1 := ((n * 64) ^ n) % 16777216
step2 := ((step1 / 32) ^ step1) % 16777216
step3 := ((step2 * 2048) ^ step2) % 16777216
mutateCache[n] = step3
return step3
}
func main() {
input := utils.ReadInput("input.txt")
mutateCache = map[int]int{}
part1(input)
part2(input)
}