Skip to content

Commit 712a775

Browse files
committed
IntCode class added (Day 5)
1 parent 7c0fd36 commit 712a775

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

2019/IntCode.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
class IntCode:
2+
instructions = []
3+
pointer = 0
4+
state = "Running"
5+
modes = "000"
6+
inputs = []
7+
outputs = []
8+
verbose_level = 0
9+
instr_length = {
10+
"01": 4,
11+
"02": 4,
12+
"03": 2,
13+
"04": 2,
14+
"05": 3,
15+
"06": 3,
16+
"07": 4,
17+
"08": 4,
18+
"99": 1,
19+
}
20+
21+
def __init__(self, instructions):
22+
self.instructions = list(map(int, instructions.split(",")))
23+
24+
def reset(self, instructions):
25+
self.instructions = list(map(int, instructions.split(",")))
26+
self.pointer = 0
27+
self.state = "Running"
28+
29+
def get_opcode(self):
30+
instr = self.instructions[self.pointer]
31+
opcode_full = "0" * (5 - len(str(instr))) + str(instr)
32+
return opcode_full
33+
34+
def get_instruction(self, opcode):
35+
return self.instructions[
36+
self.pointer : self.pointer + self.instr_length[opcode]
37+
]
38+
39+
def get_value(self, param_position):
40+
if self.modes[2 - (param_position - 1)] == "0":
41+
return self.instructions[self.instructions[self.pointer + param_position]]
42+
else:
43+
return self.instructions[self.pointer + param_position]
44+
45+
def op_01(self, instr):
46+
self.instructions[instr[3]] = self.get_value(1) + self.get_value(2)
47+
self.pointer += self.instr_length["01"]
48+
self.state = "Running"
49+
50+
def op_02(self, instr):
51+
self.instructions[instr[3]] = self.get_value(1) * self.get_value(2)
52+
self.pointer += self.instr_length["02"]
53+
self.state = "Running"
54+
55+
def op_03(self, instr):
56+
self.instructions[instr[1]] = self.inputs.pop(0)
57+
self.pointer += self.instr_length["03"]
58+
self.state = "Running"
59+
60+
def op_04(self, instr):
61+
self.outputs.append(self.get_value(1))
62+
self.pointer += self.instr_length["04"]
63+
self.state = "Running"
64+
65+
def op_05(self, instr):
66+
if self.get_value(1) != 0:
67+
self.pointer = self.get_value(2)
68+
else:
69+
self.pointer += self.instr_length["05"]
70+
self.state = "Running"
71+
72+
def op_06(self, instr):
73+
if self.get_value(1) == 0:
74+
self.pointer = self.get_value(2)
75+
else:
76+
self.pointer += self.instr_length["06"]
77+
self.state = "Running"
78+
79+
def op_07(self, instr):
80+
if self.get_value(1) < self.get_value(2):
81+
self.instructions[instr[3]] = 1
82+
else:
83+
self.instructions[instr[3]] = 0
84+
self.pointer += self.instr_length["07"]
85+
self.state = "Running"
86+
87+
def op_08(self, instr):
88+
if self.get_value(1) == self.get_value(2):
89+
self.instructions[instr[3]] = 1
90+
else:
91+
self.instructions[instr[3]] = 0
92+
self.pointer += self.instr_length["08"]
93+
self.state = "Running"
94+
95+
def op_99(self, instr):
96+
self.pointer += self.instr_length["99"]
97+
self.state = "Stopped"
98+
99+
def run(self):
100+
while self.state == "Running":
101+
opcode_full = self.get_opcode()
102+
opcode = opcode_full[-2:]
103+
self.modes = opcode_full[:-2]
104+
current_instr = self.get_instruction(opcode)
105+
if self.verbose_level >= 3:
106+
print("Executing", current_instr)
107+
print("Found opcode", opcode_full, opcode, self.modes)
108+
getattr(self, "op_" + opcode)(current_instr)
109+
if self.verbose_level >= 2:
110+
print("Pointer after execution:", self.pointer)
111+
print("Instructions:", ",".join(map(str, self.instructions)))
112+
113+
def export(self):
114+
instr = ",".join(map(str, self.instructions))
115+
inputs = ",".join(map(str, self.inputs))
116+
outputs = ",".join(map(str, self.outputs))
117+
return (
118+
"Instructions: " + instr + "\nInputs: " + inputs + "\nOutputs: " + outputs
119+
)

0 commit comments

Comments
 (0)