-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.go
216 lines (170 loc) · 3.7 KB
/
computer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"errors"
"fmt"
"strconv"
"strings"
)
const (
ADD = 1
MUL = 2
RIN = 3
PRN = 4
JIT = 5
JIF = 6
LES = 7
EQL = 8
REL = 9
BRK = 99
)
var (
ErrInvalidOpCode = errors.New("invalid opcode")
)
type Computer struct {
Input chan string
Output chan string
memory [65536]int
relativeBase int
}
func (c *Computer) Load(program []int) {
copy(c.memory[:], program)
c.Input = make(chan string)
c.Output = make(chan string)
}
func (c *Computer) Run() (err error) {
defer func() {
close(c.Input)
close(c.Output)
}()
var cursor int
for {
opCode := c.memory[cursor] % 100
modes := Reverse(PadMemory(fmt.Sprintf("%d", c.memory[cursor]/100), 4))
switch opCode {
case ADD:
operand1 := c.ReadOperand(cursor+1, modes[0])
operand2 := c.ReadOperand(cursor+2, modes[1])
c.WriteValue(cursor+3, modes[2], operand1+operand2)
cursor += 4
continue
case MUL:
operand1 := c.ReadOperand(cursor+1, modes[0])
operand2 := c.ReadOperand(cursor+2, modes[1])
c.WriteValue(cursor+3, modes[2], operand1*operand2)
cursor += 4
continue
case RIN:
val, _ := strconv.Atoi(<-c.Input)
c.WriteValue(cursor+1, modes[0], val)
cursor += 2
continue
case PRN:
operand1 := c.ReadOperand(cursor+1, modes[0])
c.Output <- fmt.Sprintf("%d", operand1)
cursor += 2
continue
case JIT:
operand1 := c.ReadOperand(cursor+1, modes[0])
if operand1 != 0 {
cursor = c.ReadOperand(cursor+2, modes[1])
continue
}
cursor += 3
continue
case JIF:
operand1 := c.ReadOperand(cursor+1, modes[0])
if operand1 == 0 {
cursor = c.ReadOperand(cursor+2, modes[1])
continue
}
cursor += 3
continue
case LES:
operand1 := c.ReadOperand(cursor+1, modes[0])
operand2 := c.ReadOperand(cursor+2, modes[1])
if operand1 < operand2 {
c.WriteValue(cursor+3, modes[2], 1)
} else {
c.WriteValue(cursor+3, modes[2], 0)
}
cursor += 4
continue
case EQL:
operand1 := c.ReadOperand(cursor+1, modes[0])
operand2 := c.ReadOperand(cursor+2, modes[1])
if operand1 == operand2 {
c.WriteValue(cursor+3, modes[2], 1)
} else {
c.WriteValue(cursor+3, modes[2], 0)
}
cursor += 4
continue
case REL:
operand1 := c.ReadOperand(cursor+1, modes[0])
c.relativeBase += operand1
cursor += 2
continue
case BRK:
fmt.Println("EXIT")
return nil
default:
return ErrInvalidOpCode
}
}
}
func (c *Computer) ReadOperand(i int, mode byte) int {
switch mode {
case '0':
return c.ReadPosition(i)
case '1':
return c.ReadImmediate(i)
case '2':
return c.ReadRelative(i)
}
panic("unknown operand mode")
}
func (c *Computer) ReadImmediate(i int) int {
return c.memory[i]
}
func (c *Computer) ReadPosition(i int) int {
return c.ReadImmediate(c.memory[i])
}
func (c *Computer) ReadRelative(i int) int {
return c.ReadImmediate(c.relativeBase + c.memory[i])
}
func (c *Computer) WriteValue(i int, mode byte, val int) {
switch mode {
case '0':
c.WritePosition(i, val)
return
case '1':
c.WriteImmediate(i, val)
return
case '2':
c.WriteRelative(i, val)
return
}
panic("unknown operand mode " + string(mode))
}
func (c *Computer) WriteImmediate(i int, val int) {
c.memory[i] = val
}
func (c *Computer) WritePosition(i int, val int) {
c.WriteImmediate(c.memory[i], val)
}
func (c *Computer) WriteRelative(i int, val int) {
c.WriteImmediate(c.relativeBase+c.memory[i], val)
}
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func PadMemory(str string, l int) string {
if len(str) < l {
return strings.Repeat("0", l-len(str)) + str
}
return str
}