aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-04 14:41:36 +0100
committerGitHub <noreply@github.com>2020-10-04 14:41:36 +0100
commit3da0c4c4d3101d43a17c96ebfb8880ebcdc0b4dd (patch)
treecb388beb63776c290976eb4490ac8740472bec8c
parente6c3a4c3849e235957d9e7f04328f0c5ff2532fd (diff)
parentabbdc7168ffedb222790a751ace33ac0f8989c7a (diff)
downloadperlweeklychallenge-club-3da0c4c4d3101d43a17c96ebfb8880ebcdc0b4dd.tar.gz
perlweeklychallenge-club-3da0c4c4d3101d43a17c96ebfb8880ebcdc0b4dd.tar.bz2
perlweeklychallenge-club-3da0c4c4d3101d43a17c96ebfb8880ebcdc0b4dd.zip
Merge pull request #2443 from simongreen-net/swg-080
sgreen solutions to challenge 080
-rw-r--r--challenge-080/sgreen/README.md4
-rw-r--r--challenge-080/sgreen/blog.txt1
-rwxr-xr-xchallenge-080/sgreen/perl/ch-1.pl27
-rwxr-xr-xchallenge-080/sgreen/perl/ch-2.pl40
4 files changed, 70 insertions, 2 deletions
diff --git a/challenge-080/sgreen/README.md b/challenge-080/sgreen/README.md
index 356818eca9..520da30452 100644
--- a/challenge-080/sgreen/README.md
+++ b/challenge-080/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 079
+# The Weekly Challenge 080
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/the-weekly-challenge-079-1jel)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/the-weekly-challenge-080-2if0)
diff --git a/challenge-080/sgreen/blog.txt b/challenge-080/sgreen/blog.txt
new file mode 100644
index 0000000000..7b808db63e
--- /dev/null
+++ b/challenge-080/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-weekly-challenge-080-2if0
diff --git a/challenge-080/sgreen/perl/ch-1.pl b/challenge-080/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..7127322050
--- /dev/null
+++ b/challenge-080/sgreen/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my @input = @_;
+
+ # Sanity checks
+ die "You must supply one or more integers\n" unless @input;
+ foreach (@input) {
+ die "The vale '$_' is not an integer\n" unless /^-?[0-9]+$/;
+ }
+
+ # Put the values in a hash for faster look up
+ my %seen = ( map { $_, undef } @input );
+
+ # Count from one until we find a value that is not in %seen
+ my $result = 1;
+ $result++ while exists $seen{$result};
+
+ # Display the result
+ say $result;
+}
+
+main(@ARGV);
diff --git a/challenge-080/sgreen/perl/ch-2.pl b/challenge-080/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..5510cc23ca
--- /dev/null
+++ b/challenge-080/sgreen/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw(say);
+
+use List::Util qw(max sum);
+
+sub main {
+ my @scores = @_;
+
+ # Sanity checks
+ die "You must supply one or more integers\n" unless @scores;
+ foreach (@scores) {
+ die "The vale '$_' is not an integer\n" unless /^-?[0-9]+$/;
+ }
+
+ my @lollies = ();
+ foreach my $col ( 0 .. $#scores ) {
+ # If this score is higher than the previous score, add one to it.
+ # Otherwise, start with 1.
+ my $left = $col != 0
+ && $scores[$col] > $scores[ $col - 1 ] ? $lollies[ $col - 1 ] + 1 : 1;
+
+ # Likewise, calculate the number of successive increases to the
+ # right (or 1 if there are none).
+ my $right = 1;
+ foreach my $col_right ( $col .. $#scores - 1 ) {
+ last if $scores[$col_right] <= $scores[ $col_right + 1 ];
+ ++$right;
+ }
+
+ # The number of lollies, is the greater of the two values.
+ push @lollies, max( $left, $right );
+ }
+
+ say 'Result is ', sum(@lollies), ' (', join( ', ', @lollies ), ')';
+}
+
+main(@ARGV);