diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-06-23 21:49:25 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-06-23 21:49:25 +0100 |
| commit | 60aad7093a0078d852ebbd0e4ec7544d733f9d04 (patch) | |
| tree | 85763a28c2918f940a3d781c840bc3f2a53e216b /challenge-118/james-smith | |
| parent | 2d43a51169fc3990ae20925bfee344d032d83fba (diff) | |
| download | perlweeklychallenge-club-60aad7093a0078d852ebbd0e4ec7544d733f9d04.tar.gz perlweeklychallenge-club-60aad7093a0078d852ebbd0e4ec7544d733f9d04.tar.bz2 perlweeklychallenge-club-60aad7093a0078d852ebbd0e4ec7544d733f9d04.zip | |
added minor optimization to trans
Diffstat (limited to 'challenge-118/james-smith')
| -rw-r--r-- | challenge-118/james-smith/perl/ch-2.pl | 19 |
1 files 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) { |
