From a4db48b4f406664028abed832272098338cc65fa Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Thu, 24 Sep 2020 14:46:52 +0100 Subject: add solution week 79 task 2 --- challenge-079/steven-wilson/perl/ch-2.pl | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 challenge-079/steven-wilson/perl/ch-2.pl 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; +} + -- cgit From 3a10e95f6942bdc0c696c3b21468109e7d84cb5d Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Thu, 24 Sep 2020 14:49:10 +0100 Subject: add blog week 079 --- challenge-079/steven-wilson/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-079/steven-wilson/blog.txt 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 -- cgit