From 60aad7093a0078d852ebbd0e4ec7544d733f9d04 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Wed, 23 Jun 2021 21:49:25 +0100 Subject: added minor optimization to trans --- challenge-118/james-smith/perl/ch-2.pl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/challenge-118/james-smith/perl/ch-2.pl b/challenge-118/james-smith/perl/ch-2.pl index 8c7c1a0163..fb4985349f 100644 --- a/challenge-118/james-smith/perl/ch-2.pl +++ b/challenge-118/james-smith/perl/ch-2.pl @@ -90,18 +90,21 @@ sub show_rt { } sub walk_trans { - my( $t, $seen, $rt ) = @_; - ## Skip if the new "chain" will be bigger than the best chain so far - ## If we have fallen off the sides of the board - ## Or if we have already visited the square. - return if $seen & ( my $v = 1 << $t ); - $seen |= $v; - $rt .= chr $t; + my( $t, $seen, $rt ) = @_; ## Current square, visited squares, current route + return if $seen & 1 << $t; ## Return if we've already been to this square. + $seen |= 1 << $t; ## Mark that we have been in this square. + $rt .= chr $t; ## Add this square to our route. return ($best_rt,$best_len) = ($rt,-1+length $rt) if ($seen & $sol) == $sol; + ## If we've found all the treasure + ## Update the best route (and it's length) + ## and return; return if $best_len <= length $rt; + ## If our route is longer than the best route + ## return; walk_trans( $_, $seen, $rt ) foreach @{$trans->[$t]}; + ## Try all knight move squares from the current + ## square. } - sub get_trans { my $q=[]; foreach my $y (0..7) { -- cgit