aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Shitov <andy@shitov.ru>2020-10-03 16:03:41 +0200
committerAndrew Shitov <andy@shitov.ru>2020-10-03 16:03:41 +0200
commit0930210a7227ffbceb8e8511e8a5ed5b73c65a52 (patch)
treefe019fb432df4d929f295878f14bab0747824c5a
parent276732afbbdfe0190806b7ac85f3c15db96d2b69 (diff)
downloadperlweeklychallenge-club-0930210a7227ffbceb8e8511e8a5ed5b73c65a52.tar.gz
perlweeklychallenge-club-0930210a7227ffbceb8e8511e8a5ed5b73c65a52.tar.bz2
perlweeklychallenge-club-0930210a7227ffbceb8e8511e8a5ed5b73c65a52.zip
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;
+}