-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.py
324 lines (286 loc) · 10.4 KB
/
Day16.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import re
import itertools
import math
def parse(filename):
graph = {"start": "AA", "edges": {}, "nodes": [], "flows": {}, "opened": []}
with open(filename, "r") as file:
for line in file:
nodes = re.findall("[A-Z][A-Z]", line)
graph["nodes"].append(nodes[0])
graph["flows"][nodes[0]] = eval(re.findall("[0-9]+", line)[0])
graph["edges"][nodes[0]] = []
for node in nodes[1:]:
graph["edges"][nodes[0]].append(node)
return graph
def count_flow(g):
sum = 0
for node in g["opened"]:
sum += g["flows"][node]
return sum
def dist(sn, en, g, nodes):
queue = [[sn, 0, []]]
visited = [sn]
while len(queue) > 0:
temp = queue.pop(0)
for path in g["edges"][temp[0]]:
if path == en:
return [temp[1] + 1, temp[2]]
if path not in visited:
queue.append([path, temp[1] + 1, [temp[2], temp[2] + [path]][path in nodes]])
visited.append(path)
return 1000
def find_min(g):
start = g["start"]
find = ["AA"]
end = {"AA": {}}
paths = {"AA": {}}
for node in g["nodes"]:
if g["flows"][node] > 0 and node not in g["opened"]:
find.append(node)
end[node] = {}
paths[node] = {}
for node in find:
skip = []
for no in end[node]:
skip.append(no)
for no in find:
if no == node or no in skip:
continue
temp = dist(node, no, g, find)
end[node][no] = temp[0]
end[no][node] = temp[0]
paths[node][no] = []
paths[no][node] = []
for node in find:
for node1 in find:
if node1 == "AA" or node1 == node:
continue
for node2 in find:
if node2 == "AA" or node2 in [node, node1]:
continue
if end[node][node2] < end[node2][node1]:
paths[node][node1].append(node2)
return paths, end
def simulate(g, plan, cost):
start = "AA"
left = 30
t_flow = 0
for t in plan:
flow = count_flow(g)
consumed_time = cost[start][t]
start = t
if left < consumed_time + 1:
break
left -= (consumed_time + 1)
t_flow += flow * (consumed_time + 1)
g["opened"].append(t)
flow = count_flow(g)
return t_flow + left * flow
def insert(candidates, value, node):
c_node = [node, value]
for i in range(len(candidates)):
if candidates[i][1] < c_node[1]:
temp = candidates[i]
candidates[i] = c_node
c_node = temp
def find_routes(nodes, g, cost):
queue = [["AA", 30, []]]
candidates = []
while len(queue) > 0:
current = queue.pop(0)
neigbours = []
neigbours_pon = {}
neigbours_neigbours_pon = {}
for node in nodes:
if node in current[2]:
continue
neigbours.append(node)
time_left = current[1] - cost[current[0]][node] - 1
if time_left < 0:
time_left = 0
neigbours_pon[node] = time_left * g["flows"][node]
neigbours_neigbours_pon[node] = 0
for mode in nodes:
if mode in current[2] or node == mode:
continue
time_left = current[1] - cost[node][mode] - 1 - cost[current[0]][node]
if time_left < 0:
time_left = 0
neigbours_neigbours_pon[node] += time_left * g["flows"][node]
cand_neigbours = []
length = math.ceil(math.sqrt(len(neigbours))*3)
cands = []
for i in range(length):
cands.append(["", 0])
for node in neigbours_pon:
temp = neigbours_pon[node] + neigbours_neigbours_pon[node]
insert(cands, temp, node)
for c in cands:
if c[0] != "":
cand_neigbours.append(c[0])
if len(cand_neigbours) == 0:
candidates.append(current[2])
for candidate in cand_neigbours:
queue.append([candidate, current[1] - cost[current[0]][candidate] - 1, current[2] + [candidate]])
return candidates
def part1(g):
paths, cost = find_min(g)
nodes = [x for x in cost]
nodes.remove("AA")
plans = find_routes(nodes, g, cost)
high = 0
i = 0
for plan in plans:
for plan in plans:
g["opened"] = []
temp = simulate(g, plan, cost)
temp = simulate(g, plan, cost)
if temp > high:
#print(temp, plan)
print(temp, plan)
high = temp
return high
def find_routes2(nodes, g, cost):
queue = [[["AA", "AA"], [26, 26], [[], []]]]
candidates = []
while len(queue) > 0:
current = queue.pop(0)
neigbours = [[], []]
neigbours_pon = [{}, {}]
neigbours_neigbours_pon = [{}, {}]
for node in nodes:
if node in current[2][0] or node in current[2][1]:
continue
neigbours[0].append(node)
neigbours[1].append(node)
time_left = [0,0]
time_left[0] = current[1][0] - cost[current[0][0]][node] - 1
time_left[1] = current[1][1] - cost[current[0][1]][node] - 1
if time_left[0] < 0:
time_left[0] = 0
if time_left[1] < 0:
time_left[1] = 0
neigbours_pon[0][node] = time_left[0] * g["flows"][node]
neigbours_pon[1][node] = time_left[1] * g["flows"][node]
neigbours_neigbours_pon[0][node] = 0
neigbours_neigbours_pon[1][node] = 0
for mode in nodes:
if mode in current[2][0] or mode in current[2][1] or node == mode:
continue
time_left[0] = current[1][0] - cost[node][mode] - 1 - cost[current[0][0]][node]
time_left[1] = current[1][1] - cost[node][mode] - 1 - cost[current[0][1]][node]
if time_left[0] < 0:
time_left[0] = 0
if time_left[1] < 0:
time_left[1] = 0
neigbours_neigbours_pon[0][node] += time_left[0] * g["flows"][mode]
neigbours_neigbours_pon[1][node] += time_left[1] * g["flows"][mode]
cand_neigbours = [[], []]
length = max(math.ceil(math.sqrt(len(neigbours))), 3)
cands = [[], []]
for i in range(length):
cands[0].append(["", 0])
cands[1].append(["", 0])
for node in neigbours_pon[0]:
temp = neigbours_pon[0][node] + neigbours_neigbours_pon[0][node]
temp1 = neigbours_pon[1][node] + neigbours_neigbours_pon[1][node]
insert(cands[0], temp, node)
insert(cands[1], temp1, node)
for i in range(len(cands[0])):
if cands[0][i][0] != "":
cand_neigbours[0].append(cands[0][i][0])
if cands[1][i][0] != "":
cand_neigbours[1].append(cands[1][i][0])
if len(cand_neigbours[1]) == 0:
candidates.append(current[2])
if len(cand_neigbours[1]) == 0 and len(cand_neigbours[0]) > 0:
for candidate in cand_neigbours[0]:
queue.append([
[candidate, current[0][1]],
[current[1][0] - cost[current[0][0]][candidate] - 1,
current[1][1]],
[current[2][0] + [candidate], current[2][1]]])
if len(cand_neigbours[0]) == 0 and len(cand_neigbours[1]) > 0:
for candidate in cand_neigbours[1]:
queue.append([
[current[0][0], candidate],
[current[1][0],
current[1][1] - cost[current[0][1]][candidate] - 1],
[current[2][0], current[2][1] + [candidate]]])
for candidate in cand_neigbours[0]:
for candi in cand_neigbours[1]:
if candi == candidate:
continue
queue.append([
[candidate, candi],
[current[1][0] - cost[current[0][0]][candidate] - 1,
current[1][1] - cost[current[0][1]][candi] - 1],
[current[2][0] + [candidate], current[2][1] + [candi]]])
return candidates
def simulate2(g, plan, cost):
start = ["AA", "AA"]
left = 26
t_flow = 0
c_moving = [0, 0]
i = j = 0
while left > 0:
t_flow += count_flow(g)
if c_moving[0] == 0 and i < len(plan[0]):
temp = plan[0][i]
c_moving[0] = cost[start[0]][temp] + 1
start[0] = temp
i+=1
if c_moving[1] == 0 and j < len(plan[1]):
temp = plan[1][j]
c_moving[1] = cost[start[1]][temp] + 1
start[1] = temp
j+=1
c_moving[1] -= 1
c_moving[0] -= 1
if c_moving[0] == 0:
g["opened"].append(start[0])
if c_moving[1] == 0:
g["opened"].append(start[1])
left -= 1
return t_flow
def part2(g):
cost = find_min(g)
nodes = [x for x in cost]
nodes.remove("AA")
plans = find_routes2(nodes, g, cost)
high = 0
for plan in plans:
g["opened"] = []
temp = simulate2(g, plan, cost)
if temp > high:
# print(temp, plan)
high = temp
return high
def test():
solution = [1651, 2640, 13468, 1288, 2400]
for i in range(5):
g = parse("Day16test" + str(i + 1) + ".txt")
temp = part1(g)
if temp == solution[i]:
print("Solution to test", i, "is correct")
else:
print("Failed test", i, "Your guess =", temp, "correct answer:", solution[i])
def real():
g = parse("Day16input.txt")
print("part1:", part1(g))
g = parse("Day16input.txt")
print("part2:", part2(g))
def test2():
solution = [1707, 2670, 12887, 1484, 3680]
for i in range(5):
g = parse("Day16test" + str(i + 1) + ".txt")
temp = part2(g)
if temp == solution[i]:
print("Solution to test", i, "is correct")
else:
print("Failed test", i, "Your guess =", temp, "correct answer:", solution[i])
#test2()
real()
# Either take a fast step to a node, cost 2 - opens vval
# Or take a short step, cost 1 - Doesn't open val
# Between 15 to 30 steps. ~ 2**23 computations That is not that bad.