This repository was archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopcode.go
More file actions
87 lines (71 loc) · 1.39 KB
/
opcode.go
File metadata and controls
87 lines (71 loc) · 1.39 KB
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
package intgode
type opcodeName int
const (
add opcodeName = iota + 1
multiply
input
output
jumpIfTrue
jumpIfFalse
lessThan
equals
relativeBaseOffset
halt = 99
)
type opcode func(*intcodeProgram)
func addOpcode(ip *intcodeProgram) {
ip.writeAt(3, ip.readAt(1)+ip.readAt(2))
ip.movePointer(4)
}
func multiplyOpcode(ip *intcodeProgram) {
ip.writeAt(3, ip.readAt(1)*ip.readAt(2))
ip.movePointer(4)
}
func inputOpcode(ip *intcodeProgram) {
ip.output <- ip.data
ip.data = []int{}
ip.writeAt(1, <-ip.input)
ip.movePointer(2)
}
func outputOpcode(ip *intcodeProgram) {
ip.data = append(ip.data, ip.readAt(1))
ip.movePointer(2)
}
func jumpIfTrueOpcode(ip *intcodeProgram) {
if ip.readAt(1) != 0 {
ip.instructionPointer = ip.readAt(2)
} else {
ip.movePointer(3)
}
}
func jumpIfFalseOpcode(ip *intcodeProgram) {
if ip.readAt(1) == 0 {
ip.instructionPointer = ip.readAt(2)
} else {
ip.movePointer(3)
}
}
func lessThanOpcode(ip *intcodeProgram) {
if ip.readAt(1) < ip.readAt(2) {
ip.writeAt(3, 1)
} else {
ip.writeAt(3, 0)
}
ip.movePointer(4)
}
func equalsOpcode(ip *intcodeProgram) {
if ip.readAt(1) == ip.readAt(2) {
ip.writeAt(3, 1)
} else {
ip.writeAt(3, 0)
}
ip.movePointer(4)
}
func relativeBaseOffsetOpcode(ip *intcodeProgram) {
ip.relativeBase += ip.readAt(1)
ip.movePointer(2)
}
func haltOpcode(ip *intcodeProgram) {
ip.halted = true
ip.output <- ip.data
}