aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Crosswhite <joel.crosswhite@ix.netcom.com>2020-12-19 07:50:56 -0700
committerJoel Crosswhite <joel.crosswhite@ix.netcom.com>2020-12-19 07:50:56 -0700
commit433ea876d5b7ed88ca03c2d3c6ee358d5ab078c6 (patch)
treeaa418fc244f9ef0d5818ff7cd16cb2308afb9435
parentdae8b253d0ac8bc1e0e46432ee81e59d07b2fa94 (diff)
downloadperlweeklychallenge-club-433ea876d5b7ed88ca03c2d3c6ee358d5ab078c6.tar.gz
perlweeklychallenge-club-433ea876d5b7ed88ca03c2d3c6ee358d5ab078c6.tar.bz2
perlweeklychallenge-club-433ea876d5b7ed88ca03c2d3c6ee358d5ab078c6.zip
Added solution for challenge 2 for week 91.
-rw-r--r--challenge-091/jcrosswh/perl/ch-2.pl62
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-091/jcrosswh/perl/ch-2.pl b/challenge-091/jcrosswh/perl/ch-2.pl
new file mode 100644
index 0000000000..5592ddba70
--- /dev/null
+++ b/challenge-091/jcrosswh/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+PWC 091 Challenge 2
+
+=head1 SYNOPSIS
+
+ $ ch-2.pl 1,2,3
+ 1
+
+ $ ch-2.pl 5,2,3
+ 0
+
+=head1 DESCRIPTION
+
+Given an array of positive numbers @N, where the value at each index determines
+how far you are allowed to jump. This script will decide if you can jump,
+exactly, to the last index. It will print 1 if you are able to reach the last
+index or 0 if you can't.
+
+=head1 SOLUTION
+
+First this script will sanatize the input, making sure that only a list of
+numbers separated by commas was passed in. The script will parse and store
+these numbers in an array. Then we will "walk" the array by trying to jump
+exactly to the end, and if that's not possible, try to jump forward, and if we
+can't do that then inform the user that it's not possible.
+
+=head1 AUTHORS
+
+Joel Crosswhite E<lt>joel.crosswhite@ix.netcom.comE<gt>
+
+=cut
+
+my $input = $ARGV[0];
+if (!defined($input) || $input !~ m/^([1-9],)*[1-9]$/) {
+ print "Usage: ch-2.pl <positive integer>,<positive integer>,...\n";
+ exit 1;
+}
+
+my @N = split(/,/, $input);
+my $length = scalar(@N);
+my $index = 0;
+
+while (1) {
+
+ my $current_number = $N[$index];
+
+ if ($current_number == $length - $index) {
+ print 1 . "\n";
+ exit 0;
+ } elsif ($current_number < $length - $index) {
+ $index += $N[$index];
+ } else {
+ print 0 . "\n";
+ exit 0;
+ }
+}