Skip to content

Commit d1bc9e6

Browse files
committed
IntCode class updated (Day 7-compatible)
1 parent 0f35a02 commit d1bc9e6

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

2019/IntCode.py

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
class IntCode:
2-
instructions = []
3-
pointer = 0
4-
state = "Running"
5-
modes = "000"
6-
inputs = []
7-
outputs = []
2+
# Verbosity
83
verbose_level = 0
4+
5+
# Count of parameters per opcode
96
instr_length = {
107
"01": 4,
118
"02": 4,
@@ -18,14 +15,38 @@ class IntCode:
1815
"99": 1,
1916
}
2017

21-
def __init__(self, instructions):
18+
def __init__(self, instructions, reference=""):
2219
self.instructions = list(map(int, instructions.split(",")))
20+
self.reference = reference
21+
22+
# Current state
23+
self.pointer = 0
24+
self.state = "Running"
25+
26+
# Current instruction modes
27+
self.modes = "000"
28+
29+
# Inputs and outputs
30+
self.inputs = []
31+
self.all_inputs = []
32+
self.outputs = []
2333

2434
def reset(self, instructions):
2535
self.instructions = list(map(int, instructions.split(",")))
2636
self.pointer = 0
2737
self.state = "Running"
2838

39+
def restart(self):
40+
self.state = "Running"
41+
42+
def add_input(self, value):
43+
try:
44+
self.inputs += value
45+
self.all_inputs += value
46+
except:
47+
self.inputs.append(value)
48+
self.all_inputs.append(value)
49+
2950
def get_opcode(self):
3051
instr = self.instructions[self.pointer]
3152
opcode_full = "0" * (5 - len(str(instr))) + str(instr)
@@ -53,6 +74,9 @@ def op_02(self, instr):
5374
self.state = "Running"
5475

5576
def op_03(self, instr):
77+
if len(self.inputs) == 0:
78+
self.state = "Paused"
79+
return
5680
self.instructions[instr[1]] = self.inputs.pop(0)
5781
self.pointer += self.instr_length["03"]
5882
self.state = "Running"
@@ -111,9 +135,18 @@ def run(self):
111135
print("Instructions:", ",".join(map(str, self.instructions)))
112136

113137
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-
)
138+
output = ""
139+
if self.reference != "":
140+
output += "Computer # " + str(self.reference)
141+
output += "\n" + "Instructions: " + ",".join(map(str, self.instructions))
142+
output += "\n" + "Inputs: " + ",".join(map(str, self.all_inputs))
143+
output += "\n" + "Outputs: " + ",".join(map(str, self.outputs))
144+
return output
145+
146+
def export_io(self):
147+
output = ""
148+
if self.reference != "":
149+
output += "Computer # " + str(self.reference)
150+
output += "\n" + "Inputs: " + ",".join(map(str, self.all_inputs))
151+
output += "\n" + "Outputs: " + ",".join(map(str, self.outputs))
152+
return output

0 commit comments

Comments
 (0)