diff options
| author | Simon Green <mail@simon.green> | 2020-10-04 20:35:51 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2020-10-04 20:35:51 +1000 |
| commit | abbdc7168ffedb222790a751ace33ac0f8989c7a (patch) | |
| tree | f514bbebc82ba501c97b221e6b5fa8c69d7b54f1 | |
| parent | aa14cbf8342e04b936f40bcc720a23a258137ecd (diff) | |
| download | perlweeklychallenge-club-abbdc7168ffedb222790a751ace33ac0f8989c7a.tar.gz perlweeklychallenge-club-abbdc7168ffedb222790a751ace33ac0f8989c7a.tar.bz2 perlweeklychallenge-club-abbdc7168ffedb222790a751ace33ac0f8989c7a.zip | |
sgreen solutions to challenge 080
| -rw-r--r-- | challenge-080/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-080/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-080/sgreen/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-080/sgreen/perl/ch-2.pl | 40 |
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); |
