aboutsummaryrefslogtreecommitdiff
path: root/challenge-079
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-09-21 20:09:43 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-09-24 14:44:30 +0200
commit6e68327a1d07c76c796e7df61f1cc19915d8540b (patch)
treefb794b8fd43310cbda3b231793281a84e041fefb /challenge-079
parentb8215db5d638c40f32f730e65be565a79961b5f1 (diff)
downloadperlweeklychallenge-club-6e68327a1d07c76c796e7df61f1cc19915d8540b.tar.gz
perlweeklychallenge-club-6e68327a1d07c76c796e7df61f1cc19915d8540b.tar.bz2
perlweeklychallenge-club-6e68327a1d07c76c796e7df61f1cc19915d8540b.zip
Solution to task 2
Diffstat (limited to 'challenge-079')
-rw-r--r--challenge-079/jo-37/perl/ch-2.pl70
1 files changed, 70 insertions, 0 deletions
diff --git a/challenge-079/jo-37/perl/ch-2.pl b/challenge-079/jo-37/perl/ch-2.pl
new file mode 100644
index 0000000000..40f585a472
--- /dev/null
+++ b/challenge-079/jo-37/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+
+use 5.014;
+use Test2::V0;
+no warnings 'experimental::smartmatch';
+use List::Util qw(uniqnum);
+
+# Count the trapped rain volume.
+sub trapped_rain_water {
+ my @hist = @_;
+
+ my $vol = 0;
+ my $prev_y;
+
+ # Iterate over the histogram at all distinct heights, increasingly.
+ for my $y (uniqnum sort {$a <=> $b} @hist) {
+ # At the floor.
+ unless (defined $prev_y) {
+ $prev_y = $y;
+ next;
+ }
+ # Get step size.
+ my $stepsize = $y - $prev_y;
+ $prev_y = $y;
+
+ my $length = 0;
+ my $leftwall = '';
+
+ # Iterate at the current height.
+ for my $x (0 .. $#hist) {
+ # Build a string representing the current position in the
+ # histogram. Depicts the walls.
+ for ($leftwall . '_' . ($hist[$x] >= $y)) {
+
+ # At first wall.
+ when ('_1') {
+ $leftwall = 1;
+ };
+
+ # After left wall: increment the length of the temporary
+ # rectangle.
+ when ('1_') {
+ $length++;
+ };
+
+ # At right wall: The temporary volume after the left
+ # wall (if any) is now trapped by a right wall. The
+ # right wall becomes the new left wall.
+ when ('1_1') {
+ $vol += $length * $stepsize;
+ $length = 0;
+ };
+ }
+ }
+ }
+
+ $vol;
+}
+
+is trapped_rain_water(2, 1, 4, 1, 2, 5), 6, 'first example';
+is trapped_rain_water(3, 1, 3, 1, 1, 5), 6, 'second example';
+# Other tests:
+is trapped_rain_water(7, 1, 1, 1, 7), 18, 'bucket';
+is trapped_rain_water(3, 2, 1, 2, 3), 4, 'gorge';
+is trapped_rain_water(1, 2, 3, 2, 1), 0, 'hill';
+is trapped_rain_water(1, 2, 3, 4, 5), 0, 'uphill';
+is trapped_rain_water(5, 4, 3, 2, 1), 0, 'downhill';
+is trapped_rain_water(1, 1, 1, 1, 1), 0, 'plain';
+
+done_testing;