aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-03 15:07:00 +0100
committerGitHub <noreply@github.com>2020-10-03 15:07:00 +0100
commit5b3a580b64473837fdb110f3cec3e1c2d78b3285 (patch)
treefe019fb432df4d929f295878f14bab0747824c5a
parent276732afbbdfe0190806b7ac85f3c15db96d2b69 (diff)
parent0930210a7227ffbceb8e8511e8a5ed5b73c65a52 (diff)
downloadperlweeklychallenge-club-5b3a580b64473837fdb110f3cec3e1c2d78b3285.tar.gz
perlweeklychallenge-club-5b3a580b64473837fdb110f3cec3e1c2d78b3285.tar.bz2
perlweeklychallenge-club-5b3a580b64473837fdb110f3cec3e1c2d78b3285.zip
Merge pull request #2436 from ash/master
ash: 080-2 (draft reference)
-rw-r--r--challenge-080/ash/raku/ch-2.raku38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-080/ash/raku/ch-2.raku b/challenge-080/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..9ad29269c6
--- /dev/null
+++ b/challenge-080/ash/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+#
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/
+
+use Test;
+
+is candies([1, 2, 2]), 5;
+is candies([1, 4, 3, 2]), 7;
+
+# candies [1, 2, 2];
+# candies [1, 4, 2, 1, 5];
+# candies [1, 2, 2, 1, 3];
+# candies [1, 4, 2, 1, 5, 2, 3, 1, 1, 3, 2];
+
+sub candies(@n) {
+ my @candies = 1 xx @n.elems;
+
+ my $s;
+
+ repeat {
+ $s = @candies.sum;
+
+ @candies[0]++ if @n[0] > @n[1] && @candies[0] <= @candies[1];
+
+ for 1 ..^ @n.end -> $i {
+ @candies[$i]++ if @n[$i] > @n[$i - 1] && @candies[$i] <= @candies[$i - 1];
+ @candies[$i]++ if @n[$i] > @n[$i + 1] && @candies[$i] <= @candies[$i + 1];
+ }
+
+ @candies[*-1]++ if (@n[*-1] > @n[*-2] && @candies[*-1] <= @candies[*-2]) || (@n[*-1] == @n[*-2] && @candies[*-1] < @candies[*-2]);
+
+ say @candies;
+
+ } until $s == @candies.sum;
+
+ return $s;
+}