diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2020-09-30 19:25:27 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2020-09-30 19:25:27 -0400 |
| commit | 6bd388f2116e5a366d242ea9f7d88411f0b3f280 (patch) | |
| tree | 01c2f6a318fd6034c4a038bc8b6dfdf4d36a4888 | |
| parent | 046b64bd51365e720de9cd521960969aafba072b (diff) | |
| download | perlweeklychallenge-club-6bd388f2116e5a366d242ea9f7d88411f0b3f280.tar.gz perlweeklychallenge-club-6bd388f2116e5a366d242ea9f7d88411f0b3f280.tar.bz2 perlweeklychallenge-club-6bd388f2116e5a366d242ea9f7d88411f0b3f280.zip | |
Challenge 80
| -rwxr-xr-x | challenge-080/dave-jacoby/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-080/dave-jacoby/perl/ch-2.pl | 29 |
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-080/dave-jacoby/perl/ch-1.pl b/challenge-080/dave-jacoby/perl/ch-1.pl new file mode 100755 index 0000000000..98df132442 --- /dev/null +++ b/challenge-080/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +use Carp; +use List::Util qw{ min max }; +use Getopt::Long; + +say spnm( 5, 2, -2, 0 ); # 1 +say spnm( 1, 8, -1 ); # 2 +say spnm( 2, 0, -1 ); # 1 +say spnm( 0 .. 12 ); # 13 + +sub spnm( @array ) { + my @list = grep { $_ > 0 } @array; # list to only positive nums + my %list = map { $_ => 1 } @list; # hash for easy lookup + my $max = 1 + max @list; # highest potential missing number + for my $i ( 1 .. $max ) { # starting from lowest potential + return $i unless $list{$i}; # return if no in lookup + } + return -1; # just in case +} + diff --git a/challenge-080/dave-jacoby/perl/ch-2.pl b/challenge-080/dave-jacoby/perl/ch-2.pl new file mode 100755 index 0000000000..b193067442 --- /dev/null +++ b/challenge-080/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +say candy_count( 1, 2, 2 ); +say candy_count( 1, 4, 3, 2 ); + +# a) You must given at least one candy to each candidate. +# b) Candidate with higher ranking get more candies than +# their immediate neighbors on either side. +sub candy_count ( @n ) { + say join '', map { '-' } @n; + say join ' ', @n; + my $total = 0; + + for my $i ( 0 .. $#n ) { + my $v = $n[$i]; + my $prev = $n[ $i - 1 ] || 0; + my $next = $n[ $i + 1 ] || 0; + $total++; # rule A + $total++ if $v > $prev && $prev > 0; # rule B.1 + $total++ if $v > $next && $next > 0; # rule B.2 + } + say ''; + return $total; +} |
