11 my ($v, $f, $n) = /Valve (..) has flow rate=(\d+);.* to valves? (.*)/;
13 $tun{$v} = [ split /, /, $n ];
19 my @q = [ $start, 0 ];
22 my ($node, $len, @path) = @$st;
24 $seen{$node} = [ $len, @path ];
26 for my $neigh (@{ $tun{$node} }) {
27 push @q, [ $neigh, $len+1, @path, $node ];
34 for my $node ('AA', keys %flow) {
35 $paths{$node} = bfs($node);
38 my @q = [ 'AA', 30, 0, { }, [ ] ];
43 my ($node, $mins, $total, $opened, $path) = @$st;
44 say "At $node $total $mins ", join(',', @$path);
48 for my $n1 (keys %{ $paths{$node} }) {
50 my $dist = $paths{$node}{$n1}[0];
51 next if $opened->{$n1};
53 my $m1 = $mins - $dist - 1;
55 my %o1 = (%$opened, $n1 => 1);
56 say " opening $n1 at $m1 ", $flow{$n1}*($m1);
57 push @q, [ $n1, $m1, $total + $flow{$n1}*($m1),
58 \%o1, [ @$path, $n1 ] ];