aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/ash
diff options
context:
space:
mode:
authorAndrew Shitov <andy@shitov.ru>2020-09-23 09:56:30 +0200
committerAndrew Shitov <andy@shitov.ru>2020-09-23 09:56:30 +0200
commit5e69fb37abe78e1d6e9f6f747f3ecf391d2492cf (patch)
tree6c54b1b6b04097311fcd618454aa705d7ff0b75a /challenge-079/ash
parentd55984d5e2507ead06af6924087bb15338094171 (diff)
downloadperlweeklychallenge-club-5e69fb37abe78e1d6e9f6f747f3ecf391d2492cf.tar.gz
perlweeklychallenge-club-5e69fb37abe78e1d6e9f6f747f3ecf391d2492cf.tar.bz2
perlweeklychallenge-club-5e69fb37abe78e1d6e9f6f747f3ecf391d2492cf.zip
ash 079-2
Diffstat (limited to 'challenge-079/ash')
-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;
+ }
+}