Advent of Code 2022 Day 16 — Mathematica
The input is small enough that brute-forcing is doable, but a best-first search would likely be faster.
input = Import@"input.txt"
lines = ToExpression@
StringCases[StringSplit[input, "\n"],
NumberString | (CharacterRange["A", "Z"] ~~
CharacterRange["A", "Z"])];
flow = Select[AssociationThread @@ Transpose@lines[[;; , 1 ;; 2]],
Positive];
d = With[{g = Graph@Flatten[Thread[#[[1]] -> #[[3 ;;]]] & /@ lines]},
AssociationThread[
VertexList@
g -> (AssociationThread[VertexList@g -> #] & /@
GraphDistanceMatrix@g)]];
f[closed_, from_, minutes_] :=
f[closed, from, minutes] =
Max[0, With[{remaining = minutes - d[from, closed[[#]]] - 1},
If[remaining > 0,
flow[closed[[#]]]*remaining +
f[Delete[closed, #], closed[[#]], remaining], Nothing]] & /@
Range@Length@closed]
f[{}, _, _] = 0;
f[Keys@flow, AA, 30]
Max[f[#, AA, 26] + f[Complement[Keys@flow, #], AA, 26] & /@
Subsets@Keys@flow]