Created
December 22, 2022 08:25
-
-
Save abeaumont/06f0dcf8f3f43e7254b9855ff36c4d25 to your computer and use it in GitHub Desktop.
Picat solution for Day 16 in Advent of Code 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import util. | |
final(30) => true. | |
final(_) => false. | |
table(+,max,nt) | |
plan({M, _, _}, P, _), final(M) => P = 0. | |
plan({M, V, N}, P, D@{G, R, NtV}), R[N] > 0, V[NtV[N]] == false ?=> | |
plan({M+1, {cond(I == NtV[N], true, V[I]) : I in 1..V.len}, N}, P1, D), | |
P = P1 + R[N] * (29 - M). | |
plan({M, V, N}, P, D@{G, R, NtV}) => | |
Ps = [], | |
foreach(N1 in G[N]) | |
plan({M+1, V, N1}, P1, D), | |
Ps := [P1 | Ps] | |
end, | |
P = max(Ps). | |
final2(26) => true. | |
final2(_) => false. | |
table(+,max,nt) | |
plan2({M, _, _, _}, P, _), final2(M) => P = 0. | |
plan2({M, V, N1, N2}, P, D@{G, R, NtV}), N1 != N2, R[N1] > 0, R[N2] > 0, V /\ (1<<NtV[N1]) == 0, V /\ (1<<NtV[N2]) == 0 ?=> | |
plan2({M+1, V \/ (1<<NtV[N1]) \/ (1<<NtV[N2]), N1, N2}, P1, D), | |
P = P1 + (R[N1] + R[N2]) * (25 - M). | |
plan2({M, V, N1, N2}, P, D@{G, R, NtV}), R[N1] > 0, V /\ (1<<NtV[N1]) == 0 ?=> | |
Ps = [], | |
V1 = V \/ (1<<NtV[N1]) , | |
foreach(N in G[N2]) | |
plan2({M+1, V1, N1, N}, P1, D), | |
Ps := [P1 | Ps] | |
end, | |
P = max(Ps) + R[N1] * (25 - M). | |
plan2({M, V, N1, N2}, P, D@{G, R, NtV}), R[N2] > 0, V /\ (1<<NtV[N2]) == 0 ?=> | |
Ps = [], | |
V1 = V \/ (1<<NtV[N2]) , | |
foreach(N in G[N1]) | |
plan2({M+1, V1, N, N2}, P1, D), | |
Ps := [P1 | Ps] | |
end, | |
P = max(Ps) + R[N2] * (25 - M). | |
plan2({M, V, N1, N2}, P, D@{G, _, _}) => | |
Ps = [], | |
foreach(N11 in G[N1], N22 in G[N2]) | |
plan2({M+1, V, N11, N22}, P1, D), | |
Ps := [P1 | Ps] | |
end, | |
P = max(Ps). | |
parse_rate(S) = R => | |
Xs = S.split("="), | |
R = Xs[2].strip(";").to_int(). | |
parse(X) = [X[2], parse_rate(X[5]), [X[I].strip(",") : I in 10..X.len]]. | |
main => | |
Nodes = [parse(L.split()) : L in read_file_lines()], | |
Names = new_map(), | |
foreach(I in 1..Nodes.len) | |
Names.put(Nodes[I, 1], I) | |
end, | |
Graph = {{Names.get(N) : N in Node[3]} : Node in Nodes}, | |
Rates = {N[2] : N in Nodes}, | |
C = 0, | |
NodesToValves = {0 : I in 1..Nodes.len}, | |
foreach(I in 1..Nodes.len, Rates[I] > 0) | |
C := C + 1, | |
NodesToValves[I] := C, | |
end, | |
Valves = {false : I in 1..C}, | |
plan({0, Valves, Names.get("AA")}, P1, {Graph, Rates, NodesToValves}), | |
plan2({0, 0, Names.get("AA"), Names.get("AA")}, P2, {Graph, Rates, NodesToValves}), | |
[P1, P2].println(). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment