day16 - just work, nothing done
This commit is contained in:
parent
145c76a583
commit
fda3e791f4
50
2022/full/day16.txt
Normal file
50
2022/full/day16.txt
Normal file
@ -0,0 +1,50 @@
|
||||
Valve QJ has flow rate=11; tunnels lead to valves HB, GL
|
||||
Valve VZ has flow rate=10; tunnel leads to valve NE
|
||||
Valve TX has flow rate=19; tunnels lead to valves MG, OQ, HM
|
||||
Valve ZI has flow rate=5; tunnels lead to valves BY, ON, RU, LF, JR
|
||||
Valve IH has flow rate=0; tunnels lead to valves YB, QS
|
||||
Valve QS has flow rate=22; tunnel leads to valve IH
|
||||
Valve QB has flow rate=0; tunnels lead to valves QX, ES
|
||||
Valve NX has flow rate=0; tunnels lead to valves UH, OP
|
||||
Valve PJ has flow rate=0; tunnels lead to valves OC, UH
|
||||
Valve OR has flow rate=6; tunnels lead to valves QH, BH, HB, JD
|
||||
Valve OC has flow rate=7; tunnels lead to valves IZ, JR, TA, ZH, PJ
|
||||
Valve UC has flow rate=0; tunnels lead to valves AA, BY
|
||||
Valve QX has flow rate=0; tunnels lead to valves AA, QB
|
||||
Valve IZ has flow rate=0; tunnels lead to valves OC, SX
|
||||
Valve AG has flow rate=13; tunnels lead to valves NW, GL, SM
|
||||
Valve ON has flow rate=0; tunnels lead to valves MO, ZI
|
||||
Valve XT has flow rate=18; tunnels lead to valves QZ, PG
|
||||
Valve AX has flow rate=0; tunnels lead to valves UH, MO
|
||||
Valve JD has flow rate=0; tunnels lead to valves OR, SM
|
||||
Valve HM has flow rate=0; tunnels lead to valves TX, QH
|
||||
Valve LF has flow rate=0; tunnels lead to valves ZI, UH
|
||||
Valve QH has flow rate=0; tunnels lead to valves OR, HM
|
||||
Valve RT has flow rate=21; tunnel leads to valve PG
|
||||
Valve NE has flow rate=0; tunnels lead to valves VZ, TA
|
||||
Valve OQ has flow rate=0; tunnels lead to valves TX, GE
|
||||
Valve AA has flow rate=0; tunnels lead to valves QZ, UC, OP, QX, EH
|
||||
Valve UH has flow rate=17; tunnels lead to valves PJ, NX, AX, LF
|
||||
Valve GE has flow rate=0; tunnels lead to valves YB, OQ
|
||||
Valve EH has flow rate=0; tunnels lead to valves AA, MO
|
||||
Valve MG has flow rate=0; tunnels lead to valves TX, NW
|
||||
Valve YB has flow rate=20; tunnels lead to valves IH, GE, XG
|
||||
Valve MO has flow rate=15; tunnels lead to valves EH, ON, AX, ZH, CB
|
||||
Valve JR has flow rate=0; tunnels lead to valves ZI, OC
|
||||
Valve GL has flow rate=0; tunnels lead to valves AG, QJ
|
||||
Valve SM has flow rate=0; tunnels lead to valves JD, AG
|
||||
Valve HB has flow rate=0; tunnels lead to valves OR, QJ
|
||||
Valve TA has flow rate=0; tunnels lead to valves OC, NE
|
||||
Valve PG has flow rate=0; tunnels lead to valves RT, XT
|
||||
Valve XG has flow rate=0; tunnels lead to valves CB, YB
|
||||
Valve ES has flow rate=9; tunnels lead to valves QB, FL
|
||||
Valve BH has flow rate=0; tunnels lead to valves RU, OR
|
||||
Valve FL has flow rate=0; tunnels lead to valves SX, ES
|
||||
Valve CB has flow rate=0; tunnels lead to valves MO, XG
|
||||
Valve QZ has flow rate=0; tunnels lead to valves AA, XT
|
||||
Valve BY has flow rate=0; tunnels lead to valves UC, ZI
|
||||
Valve ZH has flow rate=0; tunnels lead to valves MO, OC
|
||||
Valve OP has flow rate=0; tunnels lead to valves NX, AA
|
||||
Valve NW has flow rate=0; tunnels lead to valves MG, AG
|
||||
Valve RU has flow rate=0; tunnels lead to valves ZI, BH
|
||||
Valve SX has flow rate=16; tunnels lead to valves IZ, FL
|
129
2022/python/day16.py
Normal file
129
2022/python/day16.py
Normal file
@ -0,0 +1,129 @@
|
||||
import sys
|
||||
import matrix
|
||||
import shared
|
||||
import scanf
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Dict
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
@dataclass
|
||||
class Valve:
|
||||
label: str
|
||||
rate: int
|
||||
tunnels: List[str]
|
||||
opened_at: int = -1
|
||||
potential: Dict[str,int] = None
|
||||
|
||||
def set_potential(self, valves):
|
||||
self.potential = {}
|
||||
for tunnel in self.tunnels:
|
||||
self.potential[tunnel] = valves[tunnel].rate
|
||||
|
||||
def highest_potential(self):
|
||||
return max(self.potential, key=self.potential.get)
|
||||
|
||||
def parse(rows):
|
||||
valves = {}
|
||||
for row in rows:
|
||||
try:
|
||||
left,right = row.split(" valves ")
|
||||
except ValueError:
|
||||
left,right = row.split(" valve ")
|
||||
valve, rate = scanf.scanf("Valve %s has flow rate=%d; %*s %*s to", left)
|
||||
if "," in right:
|
||||
tunnels = right.split(", ")
|
||||
else:
|
||||
tunnels = [right,]
|
||||
valves[valve] = Valve(label=valve,rate=rate,tunnels=tunnels)
|
||||
|
||||
for _,v in valves.items():
|
||||
v.set_potential(valves)
|
||||
return valves
|
||||
|
||||
|
||||
|
||||
def part1(rows, sample=False):
|
||||
p1 = Part1(rows,sample, 30)
|
||||
p1.run()
|
||||
|
||||
|
||||
class Part1:
|
||||
def __init__(self, rows, sample, minutes):
|
||||
self.rows = rows
|
||||
self.sample = sample
|
||||
self.valves = parse(rows)
|
||||
self.cur = self.valves["AA"]
|
||||
self.tick = 1
|
||||
self.minutes = minutes
|
||||
|
||||
def do_tick(self, minute):
|
||||
pressure = 0
|
||||
opened = []
|
||||
for _, valve in self.valves.items():
|
||||
if valve.opened_at > 0:
|
||||
continue
|
||||
pressure += valve.rate
|
||||
print(f"== Minute {minute} == [ {self.cur.label} ]")
|
||||
print(f"\tValves {', '.join(opened)} are open, releasing {pressure} pressure")
|
||||
|
||||
|
||||
def open_or_move(self):
|
||||
#print(self.cur)
|
||||
hi = self.cur.highest_potential()
|
||||
potential_elsewhere = self.cur.potential
|
||||
pe = potential_elsewhere[hi]
|
||||
if pe > self.cur.rate:
|
||||
#print(f"should move to {hi}")
|
||||
self.move(hi)
|
||||
elif self.cur.rate > pe:
|
||||
print(f"should open {self.cur.label}")
|
||||
self.open()
|
||||
|
||||
def move(self, where):
|
||||
self.tick += 1
|
||||
self.cur = self.valves[where]
|
||||
print("\tMove to valve", where)
|
||||
|
||||
def open(self):
|
||||
if self.cur.opened_at >= 0 :
|
||||
raise Exception("tried to open valve already opened")
|
||||
self.tick += 1
|
||||
self.valves[self.cur.label].opened_at = self.tick
|
||||
self.cur = self.valves[self.cur.label]
|
||||
print(self.cur)
|
||||
|
||||
def calculate_total_flow(self):
|
||||
total = 0
|
||||
for label, valve in self.valves.items():
|
||||
if valve.is_open:
|
||||
total += valve.rate * (30 - valve.opened_at)
|
||||
return total
|
||||
|
||||
def run(self):
|
||||
for minute in range(1, self.minutes+1):
|
||||
self.do_tick(minute)
|
||||
self.open_or_move()
|
||||
|
||||
print("total flow:")
|
||||
print(self.calculate_total_flow())
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
sample = False
|
||||
if sys.argv[-1] == "--sample":
|
||||
sample = True
|
||||
rows = [row for row in shared.load_rows(16)]
|
||||
|
||||
with shared.elapsed_timer() as elapsed:
|
||||
part1(rows, sample)
|
||||
print("🕒", elapsed())
|
||||
|
||||
#with shared.elapsed_timer() as elapsed:
|
||||
# part2(rows, sample)
|
||||
# print("🕒", elapsed())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
10
2022/samples/day16.txt
Normal file
10
2022/samples/day16.txt
Normal file
@ -0,0 +1,10 @@
|
||||
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
|
||||
Valve BB has flow rate=13; tunnels lead to valves CC, AA
|
||||
Valve CC has flow rate=2; tunnels lead to valves DD, BB
|
||||
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
|
||||
Valve EE has flow rate=3; tunnels lead to valves FF, DD
|
||||
Valve FF has flow rate=0; tunnels lead to valves EE, GG
|
||||
Valve GG has flow rate=0; tunnels lead to valves FF, HH
|
||||
Valve HH has flow rate=22; tunnel leads to valve GG
|
||||
Valve II has flow rate=0; tunnels lead to valves AA, JJ
|
||||
Valve JJ has flow rate=21; tunnel leads to valve II
|
Loading…
Reference in New Issue
Block a user