From 5e69fb37abe78e1d6e9f6f747f3ecf391d2492cf Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Wed, 23 Sep 2020 09:56:30 +0200 Subject: ash 079-2 --- challenge-079/ash/raku/ch-2.raku | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-079/ash/raku/ch-2.raku 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; + } +} -- cgit