-
Notifications
You must be signed in to change notification settings - Fork 0
/
intcode.py
177 lines (173 loc) · 6.28 KB
/
intcode.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import copy
def intcode(prog, inputs):
data = prog.copy()
num = 0
base = 0
while True:
# make sure all instrctions are of length 4
inst = str(data[num])
if len(inst) < 5:
inst = '0'*(5-len(inst)) + inst
# these are for opcodes that have 2 or less params
if inst[-2:] == '03':
inputNum = inputs.popleft()
if inst[-3] == '0':
data[data[num+1]] = inputNum
else:
data[data[num+1] + base] = inputNum
num += 2
continue
elif inst[-2:] == '04':
if inst[-3] == '0':
val = data[data[num+1]]
elif inst[-3] == '1':
val = data[num+1]
else:
val = data[data[num+1] + base]
num += 2
yield val
continue
elif inst[-2:] == '09':
if inst[-3] == '0':
val = data[data[num+1]]
elif inst[-3] == '1':
val = data[num+1]
else:
val = data[data[num+1] + base]
base = base + val
num += 2
continue
elif inst[-2:] == '99':
running = False
print('Done')
break
# this part for instructions with more than 2 params
# param 1
if inst[-3] == '0':
try:
par1 = data[data[num+1]]
except IndexError:
data.extend([0 for _ in range(len(data), data[num+1] +1)])
par1 = data[data[num+1]]
elif inst[-3] == '1':
try:
par1 = data[num+1]
except IndexError:
data.extend([0 for _ in range(len(data), num+1 +1)])
par1 = data[num+1]
else:
try:
par1 = data[data[num+1] + base]
except IndexError:
data.extend([0 for _ in range(len(data), data[num+1] +base +1)])
par1 = data[data[num+1] + base]
# param 2
if inst[-4] == '0':
try:
par2 = data[data[num+2]]
except IndexError:
data.extend([0 for _ in range(len(data), data[num+2] +1)])
par2 = data[data[num+2]]
elif inst[-4] == '1':
try:
par2 = data[num+2]
except IndexError:
data.extend([0 for _ in range(len(data), num+2 +1)])
par2 = data[num+2]
else:
try:
par2 = data[data[num+2] + base]
except IndexError:
data.extend([0 for _ in range(len(data), data[num+2]+base +1)])
par2 = data[data[num+2] + base]
if inst[-2:] == '01':
val = par1 + par2
if inst[-5] == '0':
try:
data[data[num+3]] = val
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3] +1)])
data[data[num+3]] = val
else:
try:
data[data[num+3] + base] = val
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3]+base +1)])
data[data[num+3] + base] = val
elif inst[-2:] == '02':
val = par1 * par2
if inst[-5] == '0':
try:
data[data[num+3]] = val
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3] +1)])
data[data[num+3]] = val
else:
try:
data[data[num+3] + base] = val
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3]+base +1)])
data[data[num+3] + base] = val
elif inst[-2:] == '05':
if par1 != 0:
num = par2
continue
num += 3
continue
elif inst[-2:] == '06':
if par1 == 0:
num = par2
continue
num +=3
continue
elif inst[-2:] == '07':
if par1 < par2:
if inst[-5] == '0':
try:
data[data[num+3]] = 1
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3] +1)])
data[data[num+3]] = 1
else:
try:
data[data[num+3] + base] = 1
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3]+base +1)])
data[data[num+3]+base] = 1
else:
if inst[-5] == '0':
try:
data[data[num+3]] = 0
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3] +1)])
else:
try:
data[data[num+3] + base] = 0
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3]+base +1)])
elif inst[-2:] == '08':
if par1 == par2:
if inst[-5] == '0':
try:
data[data[num+3]] = 1
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3] +1)])
data[data[num+3]] = 1
else:
try:
data[data[num+3] + base] = 1
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3]+base +1)])
data[data[num+3]+base] = 1
else:
if inst[-5] == '0':
try:
data[data[num+3]] = 0
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3] +1)])
else:
try:
data[data[num+3] + base] = 0
except IndexError:
data.extend([0 for _ in range(len(data), data[num+3]+base +1)])
num += 4