closer
This commit is contained in:
parent
d7a1aac178
commit
3c195b2ea2
@ -1,4 +1,5 @@
|
||||
import sys
|
||||
import operator
|
||||
import matrix
|
||||
import shared
|
||||
import scanf
|
||||
@ -52,7 +53,7 @@ class Part1:
|
||||
self.sample = sample
|
||||
self.valves = parse(rows)
|
||||
self.nonzero = {v.label: v for _, v in self.valves.items() if v.rate > 0}
|
||||
self.cur = self.valves["AA"]
|
||||
self.cur = "AA"
|
||||
self.tick = 1
|
||||
self.minutes = minutes
|
||||
self.g = nx.DiGraph()
|
||||
@ -69,20 +70,26 @@ class Part1:
|
||||
# self.g.add_edge(lbl, t, {'weight':self.valves[t].rate})
|
||||
self.g.add_edge(lbl, t, weight=self.valves[t].rate)
|
||||
all_keys = self.valves.keys()
|
||||
|
||||
l = dict(nx.all_pairs_shortest_path_length(self.g))
|
||||
|
||||
for lbl, _ in self.valves.items():
|
||||
for other in all_keys:
|
||||
if other == lbl:
|
||||
continue
|
||||
self.path_distances[lbl][other] = min([len(x) for x in nx.shortest_simple_paths(self.g, lbl, other)])
|
||||
self.path_distances[lbl][other] = l[lbl][other]
|
||||
# min(
|
||||
# [len(x) for x in nx.shortest_simple_paths(self.g, lbl, other)]
|
||||
# )
|
||||
|
||||
def do_tick(self, minute):
|
||||
pressure = 0
|
||||
opened = []
|
||||
for _, valve in self.valves.items():
|
||||
if valve.opened_at > 0:
|
||||
if valve.opened_at >= 0:
|
||||
pressure += valve.rate
|
||||
opened.append(valve.label)
|
||||
print(f"== Min {minute}:: Valves {', '.join(opened)} are open, releasing {pressure} pressure")
|
||||
print(f"== Min {minute}:: {len(opened)} Valves {', '.join(opened)} are open, releasing {pressure} pressure")
|
||||
|
||||
def calculate_total_flow(self):
|
||||
total = 0
|
||||
@ -101,12 +108,16 @@ class Part1:
|
||||
# - Remove the recently opened valve from the closed set (functionally), so the deeper levels won't consider it
|
||||
|
||||
def priority(remaining):
|
||||
print("REMAINING", remaining)
|
||||
_pris = []
|
||||
for _,n in self.nonzero.items():
|
||||
# (time_remaining - distance_to_valve - 1) * flow rate
|
||||
pri = (remaining - self.path_distances[self.cur.label][n.label] - 1) * n.rate
|
||||
_pris.append((n.label, pri))
|
||||
_pris = list(sorted(_pris, key=lambda x: x[1], reverse=True))
|
||||
d = self.path_distances[self.cur][n.label]
|
||||
pri = (remaining - d) * n.rate
|
||||
_pris.append((n.label, pri, d))
|
||||
_pris = list(sorted(_pris, key=operator.itemgetter(2,1)))
|
||||
print(self.cur, end=' ')
|
||||
pprint(_pris)
|
||||
return _pris
|
||||
|
||||
|
||||
@ -117,19 +128,34 @@ class Part1:
|
||||
print("ran out of time")
|
||||
break
|
||||
self.do_tick(31-remaining)
|
||||
|
||||
# CALCULATE PRIORITIES
|
||||
pris = priority(remaining)
|
||||
print(pris)
|
||||
_pri, _ = pris.pop(0)
|
||||
n = self.nonzero[_pri]
|
||||
del self.nonzero[_pri]
|
||||
#print(pris)
|
||||
# GET HIGHEST PRIORITY label
|
||||
nxt, _, distance = pris.pop(0)
|
||||
#distance *= -1
|
||||
# GET HIGHEST PRIORITY VALVE
|
||||
n = self.nonzero[nxt]
|
||||
# remove valve from dict
|
||||
del self.nonzero[nxt]
|
||||
# keep track of which order opened
|
||||
open_order.append(n.label)
|
||||
print("\tMoving to", n.label)
|
||||
print("\tOpening ", n.label)
|
||||
distance = self.path_distances[self.cur.label][n.label]
|
||||
self.cur = n
|
||||
self.cur.opened_at = self.minutes - (remaining + 1)
|
||||
|
||||
print("\tMoving to", nxt)
|
||||
print("\tOpening", n.label)
|
||||
|
||||
#distance = self.path_distances[self.cur][n.label]
|
||||
self.cur = n.label
|
||||
self.valves[self.cur].opened_at = self.minutes - (remaining - 1)
|
||||
|
||||
# Tick tick tick
|
||||
remaining -= distance # Move
|
||||
print("\t\tMoved", distance,"distance/minutes")
|
||||
remaining -= 1 # open
|
||||
print("\t\tOpened",nxt,"1 minute")
|
||||
print("total flow:", self.calculate_total_flow())
|
||||
self.do_tick(30)
|
||||
|
||||
|
||||
print(remaining)
|
||||
@ -153,6 +179,9 @@ def main():
|
||||
# part2(rows, sample)
|
||||
# print("🕒", elapsed())
|
||||
|
||||
print("The result for solution 1 is: 1820")
|
||||
print("The result for solution 2 is: 2602")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user