From 5d0046af83214098fcb93ae28a1a40557a6f1844 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 19 Dec 2022 15:27:36 +0100 Subject: [PATCH] Day 19: pruning by remaining time (credits: Honza Obdrzalek) --- 2022/38.pl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/2022/38.pl b/2022/38.pl index f72f5c4..91bc477 100755 --- a/2022/38.pl +++ b/2022/38.pl @@ -25,7 +25,7 @@ while (<>) { say $prod; sub dfs($g) { - my @q = ([ 0, [ 1, 0, 0, 0 ], [ 0, 0, 0, 0 ], 0, 0 ]); + my @q = ([ 32, [ 1, 0, 0, 0 ], [ 0, 0, 0, 0 ], 0, 0 ]); my @needed; for my $rob (0 .. 3) { for my $comp (0 .. 2) { @@ -39,13 +39,15 @@ sub dfs($g) { while (@q) { my ($t, $robs, $inv, $cantbuy, $didntbuy) = @{ shift @q }; - if ($t++ == 32) { + if (!$t) { if ($inv->[3] > $mx) { $mx = $inv->[3]; say " $mx"; } next; } + next if $t*$robs->[3] + $inv->[3] + ($t*($t-1)/2) <= $mx; + $t--; my @ni = @$inv; for (0 .. 3) { @@ -57,12 +59,11 @@ sub dfs($g) { my $bp = $g->[$bpn]; my $mask = 1 << $bpn; next if $cantbuy & $mask; + next if $didntbuy & $mask; if ($bpn < 3 && $robs->[$bpn] >= $needed[$bpn]) { $cantbuy |= $mask; next; } - - next if $didntbuy & $mask; for (0 .. 2) { next ROBOT if $bp->[$_] > $inv->[$_]; } -- 2.43.5