-
Notifications
You must be signed in to change notification settings - Fork 0
/
p09.py
68 lines (58 loc) · 2.36 KB
/
p09.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
from aocd import data
from p05 import Code, IntcodeComputer
class Boost(IntcodeComputer):
def __init__(self, program):
self.rbase = 0
super().__init__(program)
self.ops.update({9: self._op_9})
def _get_args(self, op):
param = Code(self.memory[self.pointer+1])
if op.op == 3:
if op.modes[0] == '0':
return [param, None, None]
elif op.modes[0] == '2':
return [Code(param.value + self.rbase), None, None]
elif op.op in (4,9):
if op.modes[0] == '0':
return [Code(self.memory[param.value]), None, None]
elif op.modes[0] == '1':
return [Code(param.value), None, None]
elif op.modes[0] == '2':
return [Code(self.memory[param.value+self.rbase]), None, None]
elif op.op in (1, 2, 7, 8):
args = []
for param, mode in zip(self.memory[self.pointer+1:self.pointer+3], op.modes):
if mode == '0':
args.append(Code(self.memory[int(param)]))
elif mode == '1':
args.append(Code(param))
elif mode == '2':
args.append(Code(self.memory[int(param) + self.rbase]))
if op.modes[2] == '0':
args.append(Code(self.memory[self.pointer+3]))
elif op.modes[2] == '2':
args.append(Code(int(self.memory[self.pointer+3])+self.rbase))
return args
elif op.op in (5, 6):
args = []
for param, mode in zip(self.memory[self.pointer+1:self.pointer+3], op.modes):
if mode == '0':
args.append(Code(self.memory[int(param)]))
elif mode == '1':
args.append(Code(param))
elif mode == '2':
args.append(Code(self.memory[int(param) + self.rbase]))
return args + [None]
def _op_9(self, *args):
arg1, *_ = args
self.rbase += arg1.value
self.pointer += 2
# data = '109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99'
# data = '1102,34915192,34915192,7,4,7,99,0'
# data = '104,1125899906842624,99'
program = [code for code in data.split(',')]
program.extend(['0']*100000)
boost = Boost(program)
boost.execute('1')
# boost.execute('2')
print(boost.diagnostic_codes)