-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
160 lines (134 loc) · 2.75 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs(reader io.Reader, input int) int {
s := lib.ReaderToString(reader)
codes := lib.StringsToInts(strings.Split(s, ","))
state := &State{
codes: codes,
offset: 0,
over: false,
mode: 0,
input: input,
}
state.instructions = map[int]Apply{
1: state.opcode1,
2: state.opcode2,
3: state.opcode3,
4: state.opcode4,
5: state.opcode5,
6: state.opcode6,
7: state.opcode7,
8: state.opcode8,
99: state.opcode99,
}
for !state.over {
if state.offset >= len(codes) {
break
}
state.execute()
}
return state.output
}
func (s *State) execute() {
opcode := toOpcode(s.codes[s.offset])
s.instructions[opcode.opcode](opcode.ctx)
}
type Opcode struct {
opcode int
ctx Context
}
func toOpcode(i int) Opcode {
var v []int
for i != 0 {
v = append(v, i%10)
i /= 10
}
res := make([]int, 0, len(v))
for idx := 0; idx < 5-len(v); idx++ {
res = append(res, 0)
}
for idx := 0; idx < len(v); idx++ {
res = append(res, v[len(v)-idx-1])
}
return Opcode{
opcode: res[3]*10 + res[4],
ctx: Context{
modes: []int{res[2], res[1], res[0]},
},
}
}
type State struct {
codes []int
offset int
over bool
mode int
input int
output int
instructions map[int]Apply
}
type Context struct {
modes []int
}
func (s *State) getOutputIndex(param int) int {
return s.codes[s.offset+param+1]
}
func (s *State) getInput(ctx Context, param int) int {
if ctx.modes[param] == 0 {
return s.codes[s.codes[s.offset+1+param]]
}
return s.codes[s.offset+1+param]
}
type Apply func(ctx Context)
func (s *State) opcode1(ctx Context) {
s.codes[s.getOutputIndex(2)] = s.getInput(ctx, 0) + s.getInput(ctx, 1)
s.offset += 4
}
func (s *State) opcode2(ctx Context) {
s.codes[s.getOutputIndex(2)] = s.getInput(ctx, 0) * s.getInput(ctx, 1)
s.offset += 4
}
func (s *State) opcode3(ctx Context) {
s.codes[s.getOutputIndex(0)] = s.input
s.offset += 2
}
func (s *State) opcode4(ctx Context) {
s.output = s.getInput(ctx, 0)
s.offset += 2
}
func (s *State) opcode5(ctx Context) {
if s.getInput(ctx, 0) != 0 {
s.offset = s.getInput(ctx, 1)
} else {
s.offset += 3
}
}
func (s *State) opcode6(ctx Context) {
if s.getInput(ctx, 0) == 0 {
s.offset = s.getInput(ctx, 1)
} else {
s.offset += 3
}
}
func (s *State) opcode7(ctx Context) {
if s.getInput(ctx, 0) < s.getInput(ctx, 1) {
s.codes[s.getOutputIndex(2)] = 1
} else {
s.codes[s.getOutputIndex(2)] = 0
}
s.offset += 4
}
func (s *State) opcode8(ctx Context) {
if s.getInput(ctx, 0) == s.getInput(ctx, 1) {
s.codes[s.getOutputIndex(2)] = 1
} else {
s.codes[s.getOutputIndex(2)] = 0
}
s.offset += 4
}
func (s *State) opcode99(ctx Context) {
s.over = true
}