From abbdc7168ffedb222790a751ace33ac0f8989c7a Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 4 Oct 2020 20:35:51 +1000 Subject: sgreen solutions to challenge 080 --- challenge-080/sgreen/README.md | 4 ++-- challenge-080/sgreen/blog.txt | 1 + challenge-080/sgreen/perl/ch-1.pl | 27 ++++++++++++++++++++++++++ challenge-080/sgreen/perl/ch-2.pl | 40 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 challenge-080/sgreen/blog.txt create mode 100755 challenge-080/sgreen/perl/ch-1.pl create mode 100755 challenge-080/sgreen/perl/ch-2.pl 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); -- cgit