-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (124 loc) · 2.76 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("evil_input.txt")
output := doCalc(processInput(input))
strSlice := make([]string, len(output))
for i, v := range output {
strSlice[i] = strconv.Itoa(int(v))
}
ans1 := strings.Join(strSlice, ",")
fmt.Println("answer for part 1: ", ans1)
fmt.Println("answer for part 2: ", findValueOfA(processInput(input)))
}
func getComboOperand(input int64, registerA, registerB, registerC int64) int64 {
switch input {
case 0, 1, 2, 3:
return input
case 4:
return registerA
case 5:
return registerB
case 6:
return registerC
case 7:
return 7
}
return input
}
func processOpcode(operand int64, opcode int64, registerA, registerB, registerC int64, ip int64) (int64, int64, int64, int64, int64) {
comboOperand := getComboOperand(operand, registerA, registerB, registerC)
var output int64
output = -1
switch opcode {
case 0:
registerA = registerA / int64(math.Pow(2, float64(comboOperand)))
case 1:
registerB = registerB ^ operand
case 2:
registerB = (comboOperand % 8)
case 3:
if registerA != 0 {
ip = operand
}
case 4:
registerB = registerB ^ registerC
case 5:
output = int64(comboOperand % 8)
case 6:
registerB = registerA / int64(math.Pow(2, float64(comboOperand)))
case 7:
registerC = registerA / int64(math.Pow(2, float64(comboOperand)))
}
return registerA, registerB, registerC, ip, output
}
func processInput(input []string) (int64, int64, int64, []int64) {
var a, b, c int64
inst := []int64{}
for i, row := range input {
nums := aoc.FetchSliceOfIntsInString(row)
switch i {
case 0:
a = int64(nums[0])
case 1:
b = int64(nums[0])
case 2:
c = int64(nums[0])
case 4:
for _, n := range nums {
inst = append(inst, int64(n))
}
}
}
return a, b, c, inst
}
func doCalc(a, b, c int64, inst []int64) []int64 {
var ip int64
var output []int64
for ip < int64(len(inst)-1) {
var newIP, o int64
newIP = ip
o = -1
a, b, c, newIP, o = processOpcode(inst[ip+1], inst[ip+0], a, b, c, ip)
if newIP == ip {
ip += 2
} else {
ip = newIP
}
if o != -1 {
output = append(output, o)
}
}
return output
}
func findValueOfA(a, b, c int64, inst []int64) int64 {
output := []int64{}
a = 1
for {
output = doCalc(a, b, c, inst)
if reflect.DeepEqual(output, inst) {
return a
}
if len(inst) > len(output) {
a *= 2
continue
}
if len(inst) == len(output) {
for j := len(inst) - 1; j >= 0; j-- {
if inst[j] != output[j] {
// Key Insight: every nth digit increments at every 8^n th step.
// https://www.reddit.com/r/adventofcode/comments/1hg38ah/comment/m2gkd6m/
a += int64(math.Pow(8, float64(j)))
break
}
}
}
}
}