changeset 30:bf87eee356fc

day 16: solution for part 1 - yet too inefficient?
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 19 Dec 2022 21:42:08 +0100
parents 3c7df4d9b8cb
children 8590a79087a0
files 16/16.jl 16/input.txt
diffstat 2 files changed, 93 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/16/16.jl	Sat Dec 17 20:50:31 2022 +0100
+++ b/16/16.jl	Mon Dec 19 21:42:08 2022 +0100
@@ -72,32 +72,45 @@
 end
 
 # Sort key
-function expected_total_flow(time::Int, peer::Peer)::Int
-    (30 - time - peer.dist - 1) * peer.valve.flowrate
+function expected_total_flow(time::Int, node::Node, g::Dict{String,Node}, opened::Vector{String}=[])::Tuple{String,Int,Int}
+    if in(node.valve.name, opened)
+        return ("", -1, -1);
+    end
+    self = (30 - time - 1) * node.valve.flowrate;
+    push!(opened, node.valve.name);
+    opts = [(vn, p.dist, expected_total_flow(time+1+p.dist, g[vn], g, opened)[3]) for (vn, p) in node.routes if p.valve.flowrate > 0];
+    pop!(opened);
+    _, bestix = findmax(t -> t[3], opts);
+    (opts[bestix][1], opts[bestix][2], opts[bestix][3]+self)
 end
 
-function find_best_next(g::Dict{String,Node}, current::String, time::Int, opened::Set{String})::Tuple{String,Int,Int}
-    cands = [(s, p.dist, expected_total_flow(time, p)) for (s, p) = g[current].routes];
-    sort!(cands; lt=(a, b) -> a[3] < b[3], rev=true);
-    @show cands
-    for (s, dist, tf) = cands
-        if !in(s, opened)
-            return (s, dist, tf);
-        end
-    end
-end
+function trace_best_path(g::Dict{String,Node}; init="AA")
+    opened = String[]; # check later if correct.
+    active_pressure_relief = 0;
+    pressure_relieved = 0;
+    time = 0;
+    last = init;
+    total_pressure = 0;
+    while length(g) > length(opened)
+        @show (time, last, opened)
+        next, dist, score = expected_total_flow(time, g[last], g, opened);
+        @show (next, dist, score);
 
-function find_order(g::Dict{String,Node}, start::String)
-    opened = Set{String}();
-    time = 0;
-    while time < 30
-        best, dist, tf = find_best_next(g, start, time, opened);
-        println("found best: $best at $dist with tf = $tf");
-        push!(opened, best);
+        if in(next, opened)
+            break;
+        end
+
+        pressure_relieved += (dist+1) * active_pressure_relief;
+        println("Relieving $(dist+1) x $active_pressure_relief...");
+        active_pressure_relief += g[next].valve.flowrate;
+
+        push!(opened, last);
+        last = next;
         time += dist + 1;
-        println("time now: $time");
-        start = best;
     end
+    pressure_relieved += (30 - time) * active_pressure_relief;
+    println("Relieving $(30-time) x $active_pressure_relief...");
+    println("Total relief: $pressure_relieved");
 end
 
 function print_graph(g::Dict{String,Node})
@@ -114,5 +127,11 @@
 open("16/test_input.txt"; read=true) do fh
     g = build_graph(fh);
     populate_graph(g);
-    find_order(g, "AA");
+    @time trace_best_path(g);
 end
+
+open("16/input.txt"; read=true) do fh
+    g = build_graph(fh);
+    @time populate_graph(g);
+    @time trace_best_path(g);
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/16/input.txt	Mon Dec 19 21:42:08 2022 +0100
@@ -0,0 +1,52 @@
+Valve JZ has flow rate=0; tunnels lead to valves IR, LY
+Valve KD has flow rate=0; tunnels lead to valves NJ, ZS
+Valve VW has flow rate=0; tunnels lead to valves IT, VH
+Valve HS has flow rate=0; tunnels lead to valves OC, PN
+Valve EU has flow rate=19; tunnel leads to valve GQ
+Valve XF has flow rate=0; tunnels lead to valves WL, QD
+Valve DD has flow rate=8; tunnels lead to valves GQ, YY, JV, SK
+Valve TA has flow rate=0; tunnels lead to valves NJ, VJ
+Valve IR has flow rate=9; tunnels lead to valves JZ, WI, VJ, GC, WG
+Valve SS has flow rate=17; tunnels lead to valves SI, IZ, RK, WI
+Valve SG has flow rate=0; tunnels lead to valves NV, NJ
+Valve IT has flow rate=0; tunnels lead to valves LL, VW
+Valve CP has flow rate=24; tunnels lead to valves HN, ZK, EJ
+Valve SK has flow rate=0; tunnels lead to valves LL, DD
+Valve IS has flow rate=0; tunnels lead to valves AA, LL
+Valve HN has flow rate=0; tunnels lead to valves FF, CP
+Valve VH has flow rate=10; tunnels lead to valves QO, VW, RV, PN
+Valve JV has flow rate=0; tunnels lead to valves DD, RK
+Valve ZS has flow rate=0; tunnels lead to valves KD, LL
+Valve UC has flow rate=25; tunnels lead to valves JD, IV
+Valve WI has flow rate=0; tunnels lead to valves SS, IR
+Valve UR has flow rate=0; tunnels lead to valves QD, LY
+Valve GC has flow rate=0; tunnels lead to valves AA, IR
+Valve YY has flow rate=0; tunnels lead to valves DD, AA
+Valve IV has flow rate=0; tunnels lead to valves ZK, UC
+Valve BM has flow rate=0; tunnels lead to valves SA, WL
+Valve JD has flow rate=0; tunnels lead to valves IZ, UC
+Valve WL has flow rate=12; tunnels lead to valves EF, BM, EJ, XF
+Valve AA has flow rate=0; tunnels lead to valves NV, YY, GC, IS, QO
+Valve WG has flow rate=0; tunnels lead to valves LL, IR
+Valve GQ has flow rate=0; tunnels lead to valves EU, DD
+Valve SI has flow rate=0; tunnels lead to valves SS, NJ
+Valve KH has flow rate=13; tunnels lead to valves SA, ON
+Valve PC has flow rate=22; tunnel leads to valve ON
+Valve QD has flow rate=14; tunnels lead to valves XF, UR
+Valve IZ has flow rate=0; tunnels lead to valves SS, JD
+Valve QO has flow rate=0; tunnels lead to valves AA, VH
+Valve SA has flow rate=0; tunnels lead to valves BM, KH
+Valve NV has flow rate=0; tunnels lead to valves AA, SG
+Valve ZK has flow rate=0; tunnels lead to valves CP, IV
+Valve ON has flow rate=0; tunnels lead to valves PC, KH
+Valve PN has flow rate=0; tunnels lead to valves HS, VH
+Valve RV has flow rate=0; tunnels lead to valves NJ, VH
+Valve RK has flow rate=0; tunnels lead to valves SS, JV
+Valve OC has flow rate=18; tunnel leads to valve HS
+Valve EF has flow rate=0; tunnels lead to valves LY, WL
+Valve VJ has flow rate=0; tunnels lead to valves TA, IR
+Valve LL has flow rate=5; tunnels lead to valves ZS, IT, SK, IS, WG
+Valve FF has flow rate=0; tunnels lead to valves HN, LY
+Valve LY has flow rate=21; tunnels lead to valves EF, FF, UR, JZ
+Valve EJ has flow rate=0; tunnels lead to valves WL, CP
+Valve NJ has flow rate=6; tunnels lead to valves RV, KD, SG, SI, TA