-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07-part2.py
186 lines (161 loc) · 8.36 KB
/
day07-part2.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
178
179
180
181
182
183
184
185
186
program = "3,8,1001,8,10,8,105,1,0,0,21,34,51,68,89,98,179,260,341,422,99999,3,9,1001,9,4,9,102,4,9,9,4,9,99,3,9,1002,9,5,9,1001,9,2,9,1002,9,2,9,4,9,99,3,9,1001,9,3,9,102,3,9,9,101,4,9,9,4,9,99,3,9,102,2,9,9,101,2,9,9,1002,9,5,9,1001,9,2,9,4,9,99,3,9,102,2,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,99"
_data = [int(i) for i in program.split(",")]
class IP:
def __init__(self):
self.address = 0
def set(self, new_address):
self.address = new_address
def get(self):
return self.address
def add(i, a, b, c, ip):
i[c] = i[a] + i[b]
def mul(i, a, b, c, ip):
i[c] = i[a] * i[b]
def take_input(i, a, ip):
i[a] = values.pop(0)
# print("pop", i[a])
def output(i, a, ip):
try:
values[1] = i[a]
# print(values, i[a])
except:
# print(i[a])
values.append(i[a])
def jnz(i, a, b, ip):
if i[a]:
ip.set(i[b] - 3)
def jz(i, a, b, ip):
if not i[a]:
ip.set(i[b] - 3)
def tlt(i, a, b, c, ip):
if i[a] < i[b]:
i[c] = 1
else:
i[c] = 0
def teq(i, a, b, c, ip):
if i[a] == i[b]:
i[c] = 1
else:
i[c] = 0
from io import StringIO
from contextlib import redirect_stdout
from copy import deepcopy
out = StringIO()
# with redirect_stdout(out):
# for i in {0, 1, 2, 3, 4}:
# for j in {0, 1, 2, 3, 4} - {i}:
# for k in {0, 1, 2, 3, 4} - {i, j}:
# for l in {0, 1, 2, 3, 4} - {i, j, k}:
# for m in {0, 1, 2, 3, 4} - {i, j, k, l}:
# # print("---", i, j, k, l, m, "---")
# values = [i, 0, j, 0, k, 0, l, 0, m, 0]
# for _ in range(5):
# data = deepcopy(_data)
# codes = [
# lambda i, ip: None,
# add,
# mul,
# take_input,
# output,
# jnz,
# jz,
# tlt,
# teq,
# ]
# offsets = [0, 4, 4, 2, 2, 3, 3, 4, 4]
# position = IP()
# opcode = data[position.get()]
# while True:
# third_param_mode, opcode = divmod(opcode, 10000)
# second_param_mode, opcode = divmod(opcode, 1000)
# first_param_mode, opcode = divmod(opcode, 100)
# # print(opcode)
# parameters = data[
# position.get()
# + 1 : position.get()
# + offsets[opcode]
# ]
# # print(opcode, parameters)
# if first_param_mode:
# parameters[0] = data.index(parameters[0])
# if len(parameters) > 1:
# if second_param_mode:
# parameters[1] = data.index(parameters[1])
# if third_param_mode:
# parameters[2] = data.index(parameters[2])
# # print(codes[opcode].__name__, parameters)
# codes[opcode](data, *parameters, position)
# position.set(position.get() + offsets[opcode])
# opcode = data[position.get()]
# if opcode == 99:
# break
# out.seek(0)
# print(max(map(int, out.read().splitlines())))
from sys import stdout
with redirect_stdout(out):
for i in {5, 6, 7, 8, 9}:
for j in {5, 6, 7, 8, 9} - {i}:
for k in {5, 6, 7, 8, 9} - {i, j}:
for l in {5, 6, 7, 8, 9} - {i, j, k}:
for m in {5, 6, 7, 8, 9} - {i, j, k, l}:
# print("---", i, j, k, l, m, "---")
values = [i, 0, j, 0, k, 0, l, 0, m, 0]
thrusters = [_data.copy() for _ in range(5)]
thruster_ips = [IP() for _ in range(5)]
while True:
for _ in range(5):
data = thrusters[_]
codes = [
lambda i, ip: None,
add,
mul,
take_input,
output,
jnz,
jz,
tlt,
teq,
]
offsets = [0, 4, 4, 2, 2, 3, 3, 4, 4]
position = thruster_ips[_]
opcode = data[position.get()]
while True:
third_param_mode, opcode = divmod(opcode, 10000)
second_param_mode, opcode = divmod(opcode, 1000)
first_param_mode, opcode = divmod(opcode, 100)
# print(opcode)
if opcode == 99:
break
# print(
# len(data), position.get(), opcode, file=stdout
# )
parameters = data[
position.get()
+ 1 : position.get()
+ offsets[opcode]
]
# print(opcode, parameters)
if first_param_mode:
parameters[0] = data.index(parameters[0])
if len(parameters) > 1:
if second_param_mode:
parameters[1] = data.index(parameters[1])
if third_param_mode:
parameters[2] = data.index(parameters[2])
# print(codes[opcode].__name__, parameters)
codes[opcode](data, *parameters, position)
# print(opcode)
position.set(position.get() + offsets[opcode])
if opcode == 4:
break
opcode = data[position.get()]
if (opcode % 100) == 99:
break
if (opcode % 100) == 99:
break
else:
continue
break
print(values[0])
out.seek(0)
print(max(map(int, out.read().splitlines())))