aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2020-12-20 13:20:45 +0100
committerLubos Kolouch <lubos@kolouch.net>2020-12-20 13:20:45 +0100
commite7a217468d84fe485015c6011107769ccbeab8d7 (patch)
tree397a0c64a98b86e8c24cd7761efa381a5628e0e5
parentb23c5b40cfe4514bfd368437c8b23e4260fe2b79 (diff)
downloadperlweeklychallenge-club-e7a217468d84fe485015c6011107769ccbeab8d7.tar.gz
perlweeklychallenge-club-e7a217468d84fe485015c6011107769ccbeab8d7.tar.bz2
perlweeklychallenge-club-e7a217468d84fe485015c6011107769ccbeab8d7.zip
Fix challenge 2
-rw-r--r--challenge-091/lubos-kolouch/perl/ch_2.pl15
1 files changed, 10 insertions, 5 deletions
diff --git a/challenge-091/lubos-kolouch/perl/ch_2.pl b/challenge-091/lubos-kolouch/perl/ch_2.pl
index cc54941164..8c7a755298 100644
--- a/challenge-091/lubos-kolouch/perl/ch_2.pl
+++ b/challenge-091/lubos-kolouch/perl/ch_2.pl
@@ -25,13 +25,18 @@ sub jump {
my $pos = 0;
for my $num (@$in_arr) {
if ($in_arr->[$pos] == 0) {
- my $back = 0;
+ my $offset = 1;
# work backwards and stop if we can jump over or at the beginning
- while ( ($pos >= 0) and ($in_arr->[$pos] + $pos - $back > $pos) ) {
- $back--;
+ while ($pos - $offset >= 0) {
+ my $test_pos = $pos - $offset;
+
+ my $elem_at_pos = $in_arr->[$pos - $offset];
+
+ last if ($elem_at_pos + $pos - $offset > $pos);
+ $offset++;
}
- return 0 unless $back;
+ return 0 unless $pos - $offset > 0;
}
$pos++;
}
@@ -44,5 +49,5 @@ use Test::More;
is(jump([1, 2, 1, 2]), 1);
is(jump([2, 1, 1, 0, 2]), 0);
-
+is(jump([2, 1, 2, 0, 2]), 1);
done_testing;