@@ -12,6 +12,7 @@ class IntCode:
12
12
"06" : 3 ,
13
13
"07" : 4 ,
14
14
"08" : 4 ,
15
+ "09" : 2 ,
15
16
"99" : 1 ,
16
17
}
17
18
@@ -22,6 +23,7 @@ def __init__(self, instructions, reference=""):
22
23
# Current state
23
24
self .pointer = 0
24
25
self .state = "Running"
26
+ self .relative_base = 0
25
27
26
28
# Current instruction modes
27
29
self .modes = "000"
@@ -58,26 +60,71 @@ def get_instruction(self, opcode):
58
60
]
59
61
60
62
def get_value (self , param_position ):
63
+ assert self .modes [2 - (param_position - 1 )] in "012"
64
+ try :
65
+ if self .modes [2 - (param_position - 1 )] == "0" :
66
+ return self .instructions [
67
+ self .instructions [self .pointer + param_position ]
68
+ ]
69
+ elif self .modes [2 - (param_position - 1 )] == "1" :
70
+ return self .instructions [self .pointer + param_position ]
71
+ else :
72
+ return self .instructions [
73
+ self .relative_base
74
+ + self .instructions [self .pointer + param_position ]
75
+ ]
76
+ except :
77
+ return 0
78
+
79
+ def set_value (self , param_position , value ):
80
+ assert self .modes [2 - (param_position - 1 )] in "02"
61
81
if self .modes [2 - (param_position - 1 )] == "0" :
62
- return self .instructions [self .instructions [self .pointer + param_position ]]
82
+ try :
83
+ self .instructions [
84
+ self .instructions [self .pointer + param_position ]
85
+ ] = value
86
+ except :
87
+ self .instructions += [0 ] * (
88
+ self .instructions [self .pointer + param_position ]
89
+ - len (self .instructions )
90
+ + 1
91
+ )
92
+ self .instructions [
93
+ self .instructions [self .pointer + param_position ]
94
+ ] = value
63
95
else :
64
- return self .instructions [self .pointer + param_position ]
96
+ try :
97
+ self .instructions [
98
+ self .relative_base
99
+ + self .instructions [self .pointer + param_position ]
100
+ ] = value
101
+ except :
102
+ self .instructions += [0 ] * (
103
+ self .relative_base
104
+ + self .instructions [self .pointer + param_position ]
105
+ - len (self .instructions )
106
+ + 1
107
+ )
108
+ self .instructions [
109
+ self .relative_base
110
+ + self .instructions [self .pointer + param_position ]
111
+ ] = value
65
112
66
113
def op_01 (self , instr ):
67
- self .instructions [ instr [ 3 ]] = self .get_value (1 ) + self .get_value (2 )
114
+ self .set_value ( 3 , self .get_value (1 ) + self .get_value (2 ) )
68
115
self .pointer += self .instr_length ["01" ]
69
116
self .state = "Running"
70
117
71
118
def op_02 (self , instr ):
72
- self .instructions [ instr [ 3 ]] = self .get_value (1 ) * self .get_value (2 )
119
+ self .set_value ( 3 , self .get_value (1 ) * self .get_value (2 ) )
73
120
self .pointer += self .instr_length ["02" ]
74
121
self .state = "Running"
75
122
76
123
def op_03 (self , instr ):
77
124
if len (self .inputs ) == 0 :
78
125
self .state = "Paused"
79
126
return
80
- self .instructions [ instr [ 1 ]] = self .inputs .pop (0 )
127
+ self .set_value ( 1 , self .inputs .pop (0 ) )
81
128
self .pointer += self .instr_length ["03" ]
82
129
self .state = "Running"
83
130
@@ -102,20 +149,25 @@ def op_06(self, instr):
102
149
103
150
def op_07 (self , instr ):
104
151
if self .get_value (1 ) < self .get_value (2 ):
105
- self .instructions [ instr [ 3 ]] = 1
152
+ self .set_value ( 3 , 1 )
106
153
else :
107
- self .instructions [ instr [ 3 ]] = 0
154
+ self .set_value ( 3 , 0 )
108
155
self .pointer += self .instr_length ["07" ]
109
156
self .state = "Running"
110
157
111
158
def op_08 (self , instr ):
112
159
if self .get_value (1 ) == self .get_value (2 ):
113
- self .instructions [ instr [ 3 ]] = 1
160
+ self .set_value ( 3 , 1 )
114
161
else :
115
- self .instructions [ instr [ 3 ]] = 0
162
+ self .set_value ( 3 , 0 )
116
163
self .pointer += self .instr_length ["08" ]
117
164
self .state = "Running"
118
165
166
+ def op_09 (self , instr ):
167
+ self .relative_base += self .get_value (1 )
168
+ self .pointer += self .instr_length ["09" ]
169
+ self .state = "Running"
170
+
119
171
def op_99 (self , instr ):
120
172
self .pointer += self .instr_length ["99" ]
121
173
self .state = "Stopped"
@@ -139,6 +191,7 @@ def export(self):
139
191
if self .reference != "" :
140
192
output += "Computer # " + str (self .reference )
141
193
output += "\n " + "Instructions: " + "," .join (map (str , self .instructions ))
194
+ output += "\n " + "Relative base: " + str (self .relative_base )
142
195
output += "\n " + "Inputs: " + "," .join (map (str , self .all_inputs ))
143
196
output += "\n " + "Outputs: " + "," .join (map (str , self .outputs ))
144
197
return output
0 commit comments