diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-24 17:17:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-24 17:17:37 +0100 |
| commit | 95ea101dffdf30fdbcfa72c6422109514fa9e828 (patch) | |
| tree | 37c16588b930e8c8ea8d2b65cca6bbcb62cce3b8 | |
| parent | acbc3bfbc9dc7ae6212e56b6a9cb0ffe99fe11c0 (diff) | |
| parent | 3a10e95f6942bdc0c696c3b21468109e7d84cb5d (diff) | |
| download | perlweeklychallenge-club-95ea101dffdf30fdbcfa72c6422109514fa9e828.tar.gz perlweeklychallenge-club-95ea101dffdf30fdbcfa72c6422109514fa9e828.tar.bz2 perlweeklychallenge-club-95ea101dffdf30fdbcfa72c6422109514fa9e828.zip | |
Merge pull request #2368 from oWnOIzRi/week079
add solution week 79 task 2
| -rw-r--r-- | challenge-079/steven-wilson/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-079/steven-wilson/perl/ch-2.pl | 44 |
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-079/steven-wilson/blog.txt b/challenge-079/steven-wilson/blog.txt new file mode 100644 index 0000000000..b2d3c2fa80 --- /dev/null +++ b/challenge-079/steven-wilson/blog.txt @@ -0,0 +1 @@ +https://tilde.town/~wlsn/pwc079.html diff --git a/challenge-079/steven-wilson/perl/ch-2.pl b/challenge-079/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..58b775bfe8 --- /dev/null +++ b/challenge-079/steven-wilson/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw/ say /; +use List::Util qw/ sum max /; +use Test::More; + +my @N1_t = ( 2, 1, 4, 1, 2, 5 ); +my @N2_t = ( 3, 1, 3, 1, 1, 5 ); +ok( water_trapped( \@N1_t ) == 6 ); +ok( water_trapped( \@N2_t ) == 6 ); +done_testing(); + +sub water_trapped { + my $input_ref = shift; + my @input = @{$input_ref}; + my $hist_width = scalar @input; + my $hist_height = max(@input); + my $total_water_trapped; + + for my $row ( 2 .. $hist_height ) { + my @row_array; + for my $column ( 0 .. $hist_width - 1 ) { + if ( $input[$column] >= $row ) { + $row_array[$column] = 0; + } + else { + $row_array[$column] = 1; + } + } + if ( !( sum(@row_array) == $hist_width - 1 ) ) { + while ( $row_array[0] == 1 ) { + shift @row_array; + } + while ( $row_array[-1] == 1 ) { + pop @row_array; + } + $total_water_trapped += sum(@row_array); + } + } + return $total_water_trapped; +} + |
