This commit is contained in:
Tyrel Souza 2022-12-16 23:44:57 -05:00
parent d7a1aac178
commit 3c195b2ea2

View File

@ -1,4 +1,5 @@
import sys import sys
import operator
import matrix import matrix
import shared import shared
import scanf import scanf
@ -52,7 +53,7 @@ class Part1:
self.sample = sample self.sample = sample
self.valves = parse(rows) self.valves = parse(rows)
self.nonzero = {v.label: v for _, v in self.valves.items() if v.rate > 0} 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.tick = 1
self.minutes = minutes self.minutes = minutes
self.g = nx.DiGraph() 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})
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() all_keys = self.valves.keys()
l = dict(nx.all_pairs_shortest_path_length(self.g))
for lbl, _ in self.valves.items(): for lbl, _ in self.valves.items():
for other in all_keys: for other in all_keys:
if other == lbl: if other == lbl:
continue 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): def do_tick(self, minute):
pressure = 0 pressure = 0
opened = [] opened = []
for _, valve in self.valves.items(): for _, valve in self.valves.items():
if valve.opened_at > 0: if valve.opened_at >= 0:
pressure += valve.rate pressure += valve.rate
opened.append(valve.label) 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): def calculate_total_flow(self):
total = 0 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 # - Remove the recently opened valve from the closed set (functionally), so the deeper levels won't consider it
def priority(remaining): def priority(remaining):
print("REMAINING", remaining)
_pris = [] _pris = []
for _,n in self.nonzero.items(): for _,n in self.nonzero.items():
# (time_remaining - distance_to_valve - 1) * flow rate # (time_remaining - distance_to_valve - 1) * flow rate
pri = (remaining - self.path_distances[self.cur.label][n.label] - 1) * n.rate d = self.path_distances[self.cur][n.label]
_pris.append((n.label, pri)) pri = (remaining - d) * n.rate
_pris = list(sorted(_pris, key=lambda x: x[1], reverse=True)) _pris.append((n.label, pri, d))
_pris = list(sorted(_pris, key=operator.itemgetter(2,1)))
print(self.cur, end=' ')
pprint(_pris)
return _pris return _pris
@ -117,19 +128,34 @@ class Part1:
print("ran out of time") print("ran out of time")
break break
self.do_tick(31-remaining) self.do_tick(31-remaining)
# CALCULATE PRIORITIES
pris = priority(remaining) pris = priority(remaining)
print(pris) #print(pris)
_pri, _ = pris.pop(0) # GET HIGHEST PRIORITY label
n = self.nonzero[_pri] nxt, _, distance = pris.pop(0)
del self.nonzero[_pri] #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) open_order.append(n.label)
print("\tMoving to", n.label)
print("\tOpening ", n.label) print("\tMoving to", nxt)
distance = self.path_distances[self.cur.label][n.label] print("\tOpening", n.label)
self.cur = n
self.cur.opened_at = self.minutes - (remaining + 1) #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 remaining -= distance # Move
print("\t\tMoved", distance,"distance/minutes")
remaining -= 1 # open remaining -= 1 # open
print("\t\tOpened",nxt,"1 minute")
print("total flow:", self.calculate_total_flow())
self.do_tick(30)
print(remaining) print(remaining)
@ -153,6 +179,9 @@ def main():
# part2(rows, sample) # part2(rows, sample)
# print("🕒", elapsed()) # print("🕒", elapsed())
print("The result for solution 1 is: 1820")
print("The result for solution 2 is: 2602")
if __name__ == "__main__": if __name__ == "__main__":
main() main()