1
1
class IntCode :
2
- instructions = []
3
- pointer = 0
4
- state = "Running"
5
- modes = "000"
6
- inputs = []
7
- outputs = []
2
+ # Verbosity
8
3
verbose_level = 0
4
+
5
+ # Count of parameters per opcode
9
6
instr_length = {
10
7
"01" : 4 ,
11
8
"02" : 4 ,
@@ -18,14 +15,38 @@ class IntCode:
18
15
"99" : 1 ,
19
16
}
20
17
21
- def __init__ (self , instructions ):
18
+ def __init__ (self , instructions , reference = "" ):
22
19
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 = []
23
33
24
34
def reset (self , instructions ):
25
35
self .instructions = list (map (int , instructions .split ("," )))
26
36
self .pointer = 0
27
37
self .state = "Running"
28
38
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
+
29
50
def get_opcode (self ):
30
51
instr = self .instructions [self .pointer ]
31
52
opcode_full = "0" * (5 - len (str (instr ))) + str (instr )
@@ -53,6 +74,9 @@ def op_02(self, instr):
53
74
self .state = "Running"
54
75
55
76
def op_03 (self , instr ):
77
+ if len (self .inputs ) == 0 :
78
+ self .state = "Paused"
79
+ return
56
80
self .instructions [instr [1 ]] = self .inputs .pop (0 )
57
81
self .pointer += self .instr_length ["03" ]
58
82
self .state = "Running"
@@ -111,9 +135,18 @@ def run(self):
111
135
print ("Instructions:" , "," .join (map (str , self .instructions )))
112
136
113
137
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 + "\n Inputs: " + inputs + "\n Outputs: " + 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