aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-079/ash/raku/ch-2.raku32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-079/ash/raku/ch-2.raku b/challenge-079/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..d04ce7a257
--- /dev/null
+++ b/challenge-079/ash/raku/ch-2.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+#
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/
+
+my @shape = 2, 1, 4, 1, 2, 5;
+
+# Make the matrix and fill it with '#' (bricks) and '~' (water);
+my $height = max(@shape);
+my @map;
+for @shape -> $cell {
+ push @map, Array.new(('#' xx $cell, '~' xx ($height - $cell)).flat);
+}
+
+blow-water-out(0, @shape.end);
+blow-water-out(@shape.end, 0);
+
+.join.say for @map;
+
+# Units of water left
+say @map.join.join.indices('~').elems;
+
+sub blow-water-out($from, $to) {
+ # Blow from left or from right to remove high waters.
+ my $max = @shape[$from]; # Height of the left-most (or right-most) cell
+ for $from ... $to -> $pos {
+ $max = @shape[$pos] if @shape[$pos] > $max; # Raising wall, increase the maximum
+
+ # Remove water above the current maximum and fill it with air
+ @map[$pos; $_] = '.' for $max ..^ $height;
+ }
+}