diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-23 08:58:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-23 08:58:54 +0100 |
| commit | 547f828e979935ca0c67878648a3967ee0e395fe (patch) | |
| tree | 1fa8c76141059415d15f4844886b7c793cf05b3f | |
| parent | d9c14d193442b4aedc36243aa28acf86fefb56a7 (diff) | |
| parent | 5e69fb37abe78e1d6e9f6f747f3ecf391d2492cf (diff) | |
| download | perlweeklychallenge-club-547f828e979935ca0c67878648a3967ee0e395fe.tar.gz perlweeklychallenge-club-547f828e979935ca0c67878648a3967ee0e395fe.tar.bz2 perlweeklychallenge-club-547f828e979935ca0c67878648a3967ee0e395fe.zip | |
Merge pull request #2357 from ash/master
ash 079-2
| -rw-r--r-- | challenge-079/ash/raku/ch-2.raku | 32 |
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; + } +} |
