aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2020-08-26 20:52:46 -0400
committerWalt Mankowski <waltman@pobox.com>2020-08-26 20:52:46 -0400
commitf09ff0b5b57f38aa13376ffd3cd5d642c8d75a3b (patch)
treedd13e99e6d9bfb63a08df7117c24caa5a0b8ba34
parent59f42083ee3c1c04bf9db831430d5b9d94c446a2 (diff)
downloadperlweeklychallenge-club-f09ff0b5b57f38aa13376ffd3cd5d642c8d75a3b.tar.gz
perlweeklychallenge-club-f09ff0b5b57f38aa13376ffd3cd5d642c8d75a3b.tar.bz2
perlweeklychallenge-club-f09ff0b5b57f38aa13376ffd3cd5d642c8d75a3b.zip
changed to use negative array indexing
I did this in the Python version and it made the code a bit easier to follow
-rw-r--r--challenge-075/walt-mankowski/perl/ch-1.pl13
1 files changed, 6 insertions, 7 deletions
diff --git a/challenge-075/walt-mankowski/perl/ch-1.pl b/challenge-075/walt-mankowski/perl/ch-1.pl
index 556d2fd271..032d6d598b 100644
--- a/challenge-075/walt-mankowski/perl/ch-1.pl
+++ b/challenge-075/walt-mankowski/perl/ch-1.pl
@@ -30,7 +30,6 @@ my @c = @ARGV;
my @solutions;
my @cnt = map {0} 0..$#c;
-my $i = $#c;
while (1) {
my $val = value(\@c, \@cnt);
if ($val >= $s) {
@@ -40,17 +39,17 @@ while (1) {
}
# rotate "odometer"
- $cnt[$#c] = 0;
- my $j = $#c - 1;
+ $cnt[-1] = 0;
+ my $j = -2;
$cnt[$j]++;
- while ($j >= 0 && value(\@c, \@cnt) > $s) {
+ while ($j >= -@c && value(\@c, \@cnt) > $s) {
$cnt[$j] = 0;
$j--;
- $cnt[$j]++;
+ $cnt[$j]++ if $j >= -@c;
}
- last if $j < 0;
+ last if $j < -@c;
} else {
- $cnt[$#c]++;
+ $cnt[-1]++;
}
}