Add ugly day 16, to be improved...
This commit is contained in:
parent
48e8dff52b
commit
3d383527e4
105
2022/day16.py
Normal file
105
2022/day16.py
Normal file
@ -0,0 +1,105 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
import itertools
|
||||
import re
|
||||
import sys
|
||||
|
||||
from docplex.mp.model import Model
|
||||
|
||||
lines = sys.stdin.read().splitlines()
|
||||
|
||||
pipes: dict[str, tuple[int, list[str]]] = {}
|
||||
for line in lines:
|
||||
r = re.match(
|
||||
R"Valve ([A-Z]+) has flow rate=([0-9]+); tunnels? leads? to valves? (.+)",
|
||||
line,
|
||||
).groups()
|
||||
|
||||
pipes[r[0]] = (int(r[1]), r[2].split(", "))
|
||||
|
||||
|
||||
start_pipe = "AA"
|
||||
max_time = 30
|
||||
|
||||
#
|
||||
max_time = 26
|
||||
ee = [0, 1]
|
||||
|
||||
m = Model()
|
||||
|
||||
present_at = m.binary_var_dict(
|
||||
(e, t, pipe) for e, t, pipe in itertools.product(ee, range(max_time), pipes)
|
||||
)
|
||||
open_at = m.binary_var_dict(
|
||||
(e, t, pipe) for e, t, pipe in itertools.product(ee, range(max_time), pipes)
|
||||
)
|
||||
|
||||
for e in ee:
|
||||
m.add_constraint(present_at[e, 0, start_pipe] == 1)
|
||||
|
||||
for e, pipe in itertools.product(ee, pipes):
|
||||
m.add_constraint(open_at[e, 0, pipe] == 0)
|
||||
|
||||
for e, t in itertools.product(ee, range(max_time)):
|
||||
m.add_constraint(m.sum(present_at[e, t, pipe] for pipe in pipes) == 1)
|
||||
|
||||
for e, t, pipe in itertools.product(ee, range(1, max_time), pipes):
|
||||
m.add_constraint(
|
||||
present_at[e, t, pipe]
|
||||
<= present_at[e, t - 1, pipe]
|
||||
+ m.sum(present_at[e, t - 1, pipe2] for pipe2 in pipes[pipe][1])
|
||||
)
|
||||
|
||||
for pipe2 in pipes:
|
||||
if pipe2 != pipe:
|
||||
m.add_constraint(present_at[e, t, pipe] <= 1 - open_at[e, t - 1, pipe2])
|
||||
|
||||
for e, t, pipe in itertools.product(ee, range(1, max_time), pipes):
|
||||
m.add_constraint(open_at[e, t, pipe] <= present_at[e, t, pipe])
|
||||
|
||||
# keeps flowing
|
||||
flowing_at = m.binary_var_dict(
|
||||
(t, pipe) for t, pipe in itertools.product(range(max_time), pipes)
|
||||
)
|
||||
|
||||
for t, pipe in itertools.product(range(max_time), pipes):
|
||||
m.add_constraint(
|
||||
flowing_at[t, pipe]
|
||||
== m.sum(open_at[e, t2, pipe] for e, t2 in itertools.product(ee, range(0, t)))
|
||||
)
|
||||
|
||||
|
||||
# objective
|
||||
m.set_objective(
|
||||
"max",
|
||||
m.sum(
|
||||
flowing_at[t, pipe] * pipes[pipe][0]
|
||||
for t, pipe in itertools.product(range(max_time), pipes)
|
||||
),
|
||||
)
|
||||
|
||||
m.log_output = True
|
||||
s = m.solve()
|
||||
|
||||
print(s.get_objective_value())
|
||||
|
||||
|
||||
# for t in range(max_time):
|
||||
# present = {
|
||||
# e: [pipe for pipe in pipes if s.get_value(present_at[e, t, pipe]) > 1e-6]
|
||||
# for e in ee
|
||||
# }
|
||||
# opent = {
|
||||
# e: [pipe for pipe in pipes if s.get_value(open_at[e, t, pipe]) > 1e-6]
|
||||
# for e in ee
|
||||
# }
|
||||
# flowing = [
|
||||
# pipe
|
||||
# for pipe in pipes
|
||||
# if any(s.get_value(flowing_at[e, t, pipe]) > 1e-6 for e in ee)
|
||||
# ]
|
||||
|
||||
# p = [present[e][0] for e in ee]
|
||||
# o = [opent[e][0] if opent[e] else "-" for e in ee]
|
||||
|
||||
# print(f"t={t}, at={p}, open={o}, flowing={flowing}")
|
64
2022/inputs/day16.txt
Normal file
64
2022/inputs/day16.txt
Normal file
@ -0,0 +1,64 @@
|
||||
Valve MU has flow rate=0; tunnels lead to valves VT, LA
|
||||
Valve TQ has flow rate=0; tunnels lead to valves HU, SU
|
||||
Valve YH has flow rate=0; tunnels lead to valves CN, BN
|
||||
Valve EO has flow rate=0; tunnels lead to valves IK, CN
|
||||
Valve MH has flow rate=0; tunnels lead to valves GG, HG
|
||||
Valve RJ has flow rate=0; tunnels lead to valves AA, RI
|
||||
Valve XZ has flow rate=0; tunnels lead to valves PX, VT
|
||||
Valve UU has flow rate=0; tunnels lead to valves DT, XG
|
||||
Valve KV has flow rate=13; tunnels lead to valves HN, CV, PE, XD, TA
|
||||
Valve SU has flow rate=19; tunnels lead to valves TQ, HF, OL, SF
|
||||
Valve BB has flow rate=0; tunnels lead to valves NS, HR
|
||||
Valve RI has flow rate=4; tunnels lead to valves ML, EE, TZ, RJ, PE
|
||||
Valve TZ has flow rate=0; tunnels lead to valves VT, RI
|
||||
Valve LY has flow rate=0; tunnels lead to valves EE, RP
|
||||
Valve PX has flow rate=0; tunnels lead to valves XZ, JQ
|
||||
Valve VH has flow rate=0; tunnels lead to valves DT, TA
|
||||
Valve HN has flow rate=0; tunnels lead to valves KV, LR
|
||||
Valve LR has flow rate=0; tunnels lead to valves HR, HN
|
||||
Valve NJ has flow rate=0; tunnels lead to valves QF, JC
|
||||
Valve AM has flow rate=0; tunnels lead to valves OJ, AA
|
||||
Valve FM has flow rate=0; tunnels lead to valves VT, RP
|
||||
Valve VT has flow rate=5; tunnels lead to valves IP, XZ, TZ, FM, MU
|
||||
Valve HF has flow rate=0; tunnels lead to valves NR, SU
|
||||
Valve HR has flow rate=11; tunnels lead to valves BB, KO, LR
|
||||
Valve WX has flow rate=0; tunnels lead to valves CN, IP
|
||||
Valve PE has flow rate=0; tunnels lead to valves KV, RI
|
||||
Valve QF has flow rate=17; tunnels lead to valves YI, NJ
|
||||
Valve EE has flow rate=0; tunnels lead to valves LY, RI
|
||||
Valve UH has flow rate=25; tunnel leads to valve YI
|
||||
Valve CV has flow rate=0; tunnels lead to valves KV, NS
|
||||
Valve SF has flow rate=0; tunnels lead to valves YN, SU
|
||||
Valve RP has flow rate=3; tunnels lead to valves HG, FM, OJ, IK, LY
|
||||
Valve XD has flow rate=0; tunnels lead to valves IL, KV
|
||||
Valve GG has flow rate=12; tunnels lead to valves ML, IL, MH, OL, KA
|
||||
Valve XG has flow rate=0; tunnels lead to valves LI, UU
|
||||
Valve YA has flow rate=21; tunnels lead to valves UJ, GQ
|
||||
Valve OL has flow rate=0; tunnels lead to valves GG, SU
|
||||
Valve AN has flow rate=0; tunnels lead to valves AA, IX
|
||||
Valve LI has flow rate=15; tunnel leads to valve XG
|
||||
Valve GQ has flow rate=0; tunnels lead to valves YA, KO
|
||||
Valve HU has flow rate=0; tunnels lead to valves TQ, DT
|
||||
Valve OJ has flow rate=0; tunnels lead to valves RP, AM
|
||||
Valve YN has flow rate=0; tunnels lead to valves SF, JQ
|
||||
Valve ML has flow rate=0; tunnels lead to valves RI, GG
|
||||
Valve UJ has flow rate=0; tunnels lead to valves YA, NS
|
||||
Valve IX has flow rate=0; tunnels lead to valves AN, JQ
|
||||
Valve JC has flow rate=0; tunnels lead to valves JQ, NJ
|
||||
Valve TA has flow rate=0; tunnels lead to valves KV, VH
|
||||
Valve DT has flow rate=16; tunnels lead to valves UU, HU, KA, VH
|
||||
Valve NR has flow rate=0; tunnels lead to valves HF, CN
|
||||
Valve YI has flow rate=0; tunnels lead to valves QF, UH
|
||||
Valve AA has flow rate=0; tunnels lead to valves AM, AN, BN, LA, RJ
|
||||
Valve BN has flow rate=0; tunnels lead to valves AA, YH
|
||||
Valve KA has flow rate=0; tunnels lead to valves GG, DT
|
||||
Valve IL has flow rate=0; tunnels lead to valves GG, XD
|
||||
Valve CN has flow rate=7; tunnels lead to valves YH, EO, WX, NR, OM
|
||||
Valve IP has flow rate=0; tunnels lead to valves WX, VT
|
||||
Valve OM has flow rate=0; tunnels lead to valves CN, JQ
|
||||
Valve KO has flow rate=0; tunnels lead to valves GQ, HR
|
||||
Valve LA has flow rate=0; tunnels lead to valves AA, MU
|
||||
Valve JQ has flow rate=6; tunnels lead to valves IX, JC, PX, YN, OM
|
||||
Valve IK has flow rate=0; tunnels lead to valves EO, RP
|
||||
Valve HG has flow rate=0; tunnels lead to valves MH, RP
|
||||
Valve NS has flow rate=23; tunnels lead to valves CV, BB, UJ
|
Loading…
Reference in New Issue
Block a user