diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-09-28 11:01:04 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2020-09-29 20:52:05 +0200 |
| commit | 7be7bfd1cee807bf8c280337e33f76f9a3b50fbb (patch) | |
| tree | 08f14075e2e454a02683b4652cc1174164806d1e /challenge-080/jo-37 | |
| parent | 98cd7793d3806a438785d925b92b1353ef2219e9 (diff) | |
| download | perlweeklychallenge-club-7be7bfd1cee807bf8c280337e33f76f9a3b50fbb.tar.gz perlweeklychallenge-club-7be7bfd1cee807bf8c280337e33f76f9a3b50fbb.tar.bz2 perlweeklychallenge-club-7be7bfd1cee807bf8c280337e33f76f9a3b50fbb.zip | |
Solution to task 2
Diffstat (limited to 'challenge-080/jo-37')
| -rwxr-xr-x | challenge-080/jo-37/perl/ch-2.pl | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-080/jo-37/perl/ch-2.pl b/challenge-080/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..95c695830f --- /dev/null +++ b/challenge-080/jo-37/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +use Test2::V0; +use List::Util qw(max sum); +use Data::Dump; + +my $verbose = 1; + +# Distribution of candies amongst candidates. +# The task's description together with its examples look somehow +# obscure. Tried to implement something reasonable. +sub candies { + + # Initially all candidates get one candy. + my @candies = (1) x @_; + + # Forward: If the candidate has a higher ranking than her left + # neighbor, she gets one more candy than he. + foreach (1 .. $#_) { + $candies[$_] = $candies[$_ - 1] + 1 + if $_[$_] > $_[$_ - 1]; + } + + # Backward: If the candidate has a higher ranking than his right + # neighbor, he gets at least one more candy than she. + # The usage of negative subscrips might be confusing but achieves + # symmetry to the forward block. + foreach (2 .. @_) { + $candies[-$_] = max $candies[-$_], $candies[-$_ + 1] + 1 + if $_[-$_] > $_[-$_ + 1]; + } + + # The number of candies alone does not provide much insight. + if ($verbose) { + dd @_; + dd @candies; + } + + sum @candies; +} + +is candies(1, 2, 2), 4, 'first example'; +is candies(1, 4, 3, 2), 7, 'second example'; +is candies(1), 1, 'the lonely candidate'; +is candies(1, 2, 3, 4, 1), 11, 'step up'; +is candies(1, 4, 3, 2, 1), 11, 'step down'; +is candies(1, 2, 3, 4, 3, 2), 13, 'right peak'; +is candies(2, 3, 4, 3, 2, 1), 13, 'left peak'; +is candies(1, 2, 3, 3, 3, 2, 1), 13, 'the unlucky fellow'; + +done_testing; |
