-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.py
144 lines (125 loc) · 4.12 KB
/
instruction.py
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
import typing
from enum import IntEnum
class AddressingMode(IntEnum):
DIRECT = 0 # Operand is an address of the value
IMMEDIATE = 1 # Operand is the value
RELATIVE = 2
class IntcodeInstruction:
def __init__(self, *args):
assert len(args) == self.OPERANDS
self.args = args
def expand(self):
return (
self.OPCODE +
IntcodeInstruction.encode_modes([
AddressingMode.IMMEDIATE if type(a) == str else AddressingMode.DIRECT
for a in self.args
]),
*map(int, self.args)
)
@staticmethod
def exec(full_opcode : int, machine, *args):
raise NotImplemented()
@staticmethod
def decode_modes(full_opcode : int) -> typing.Sequence[AddressingMode]:
m_str = f"{full_opcode//100:03}"
return tuple(AddressingMode(int(c)) for c in m_str[::-1])
@staticmethod
def encode_modes(modes : typing.Sequence[AddressingMode]):
result = 0
for i in range(len(modes)):
result += modes[i] * 10**(2+i)
return result
class IAdd(IntcodeInstruction):
OPCODE = 1
OPERANDS = 3
@staticmethod
def exec(full_opcode : int, machine, op0, op1, op2):
modes = IntcodeInstruction.decode_modes(full_opcode)
machine.store(
op2,
machine.fetch(op0, modes[0]) +
machine.fetch(op1, modes[1]),
modes[2]
)
class IMult(IntcodeInstruction):
OPCODE = 2
OPERANDS = 3
@staticmethod
def exec(full_opcode : int, machine, op0, op1, op2):
modes = IntcodeInstruction.decode_modes(full_opcode)
machine.store(
op2,
machine.fetch(op0, modes[0]) *
machine.fetch(op1, modes[1]),
modes[2]
)
class IHalt(IntcodeInstruction):
OPCODE = 99
OPERANDS = 0
@staticmethod
def exec(full_opcode : int, machine):
machine.running = False
class IInput(IntcodeInstruction):
OPCODE = 3
OPERANDS = 1
@staticmethod
def exec(full_opcode : int, machine, op):
mode = IntcodeInstruction.decode_modes(full_opcode)[0]
if len(machine.input) > 0:
val = machine.input.pop(0)
machine.store(op, val, mode)
else:
machine.pc -= 2
raise EOFError("Out of input!")
class IOutput(IntcodeInstruction):
OPCODE = 4
OPERANDS = 1
@staticmethod
def exec(full_opcode : int, machine, op):
modes = IntcodeInstruction.decode_modes(full_opcode)
val = machine.fetch(op, modes[0])
machine.output.append(val)
class IJumpNZ(IntcodeInstruction):
OPCODE = 5
OPERANDS = 2
@staticmethod
def exec(full_opcode : int, machine, op0, op1):
modes = IntcodeInstruction.decode_modes(full_opcode)
if machine.fetch(op0, modes[0]) != 0:
machine.jump(machine.fetch(op1, modes[1]))
class IJumpZ(IntcodeInstruction):
OPCODE = 6
OPERANDS = 2
@staticmethod
def exec(full_opcode : int, machine, op0, op1):
modes = IntcodeInstruction.decode_modes(full_opcode)
if machine.fetch(op0, modes[0]) == 0:
machine.jump(machine.fetch(op1, modes[1]))
class ILessThan(IntcodeInstruction):
OPCODE = 7
OPERANDS = 3
@staticmethod
def exec(full_opcode : int, machine, op0, op1, op2):
modes = IntcodeInstruction.decode_modes(full_opcode)
if machine.fetch(op0, modes[0]) < machine.fetch(op1, modes[1]):
val = 1
else: val = 0
machine.store(op2, val, modes[2])
class IEquals(IntcodeInstruction):
OPCODE = 8
OPERANDS = 3
@staticmethod
def exec(full_opcode : int, machine, op0, op1, op2):
modes = IntcodeInstruction.decode_modes(full_opcode)
if machine.fetch(op0, modes[0]) == machine.fetch(op1, modes[1]):
val = 1
else: val = 0
machine.store(op2, val, modes[2])
class IAdjustOffset(IntcodeInstruction):
OPCODE = 9
OPERANDS = 1
@staticmethod
def exec(full_opcode : int, machine, op):
mode = IntcodeInstruction.decode_modes(full_opcode)[0]
machine.relbase += machine.fetch(op,mode)