diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-14 21:55:52 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-07-14 21:55:52 +0100 |
| commit | 273b446fb3e363c113fc4c4e6eec0e2d04aacbba (patch) | |
| tree | 2368d2d6062e663d4b3e36aced9a94e7901058fc | |
| parent | bbf09073ce0c20a456e3134885d24e2c4a4b28d3 (diff) | |
| download | perlweeklychallenge-club-273b446fb3e363c113fc4c4e6eec0e2d04aacbba.tar.gz perlweeklychallenge-club-273b446fb3e363c113fc4c4e6eec0e2d04aacbba.tar.bz2 perlweeklychallenge-club-273b446fb3e363c113fc4c4e6eec0e2d04aacbba.zip | |
- Updated Perl solution by Ulrich Rieke.
| -rw-r--r-- | challenge-121/ulrich-rieke/perl/ch-2.pl | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/challenge-121/ulrich-rieke/perl/ch-2.pl b/challenge-121/ulrich-rieke/perl/ch-2.pl index 73eec14ded..dcea569ba9 100644 --- a/challenge-121/ulrich-rieke/perl/ch-2.pl +++ b/challenge-121/ulrich-rieke/perl/ch-2.pl @@ -2,7 +2,6 @@ use strict ; use warnings ; use feature 'say' ; -use List::Util qw ( sum ) ; #find a number in a row sub findColumn { @@ -34,21 +33,23 @@ sub findNextCity { } my @matrix = ( [0, 5, 2, 7] , [5, 0, 5, 3] , [3, 1, 0, 6] , [4, 5, 4, 0] ) ; -my @tour ; +my @tour ; #the places we touch my $len = scalar @matrix ; -my %visited ; +my %visited ; #where we have been +my $length = 0 ; #total length covered my $startPlace = 0 ; my $currentPlace = $startPlace ; $visited{ $startPlace }++ ; push @tour , 0 ; while ( scalar ( keys %visited ) < $len ) { my $column = findNextCity( $matrix[ $currentPlace ] , \%visited ) ; - push @tour , $matrix[ $currentPlace ][$column] ; + $length += $matrix[ $currentPlace ][ $column ] ; + push @tour , $column ; $currentPlace = $column ; $visited{ $currentPlace }++ ; } #we must return to the start -push @tour , $matrix[ $currentPlace ][$startPlace] ; -my $length = sum ( @tour ) ; +push @tour , 0 ; +$length += $matrix[ $currentPlace ][ 0 ] ; say "length = $length" ; say "tour = (" . join( ' ' , @tour ) . ")" ; |
