diff options
| author | Abigail <abigail@abigail.be> | 2020-09-21 18:43:32 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2020-09-21 18:43:32 +0200 |
| commit | 5b410e796c910a170f80bc40c4a91b0b6f1f2edf (patch) | |
| tree | 0a981957583f2be9f8dcf0d429ac5b9f71be7d7e /challenge-079/abigail | |
| parent | e922cd84165fccaf061431d8fdb6c7a36c56b0fb (diff) | |
| download | perlweeklychallenge-club-5b410e796c910a170f80bc40c4a91b0b6f1f2edf.tar.gz perlweeklychallenge-club-5b410e796c910a170f80bc40c4a91b0b6f1f2edf.tar.bz2 perlweeklychallenge-club-5b410e796c910a170f80bc40c4a91b0b6f1f2edf.zip | |
Perl solution for Week 79, part 2.
Diffstat (limited to 'challenge-079/abigail')
| -rw-r--r-- | challenge-079/abigail/perl/ch-2.pl | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-079/abigail/perl/ch-2.pl b/challenge-079/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..3fc6b9fd7e --- /dev/null +++ b/challenge-079/abigail/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/opt/perl/bin/perl + +use 5.032; + +# +# Challenge: +# +# You are given an array of positive numbers @N. +# Write a script to represent it as Histogram Chart and find out +# how much water it can trap. +# + +use strict; +use warnings; +no warnings 'syntax'; + +use List::Util qw [max]; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# Read the input, extract the numbers, store them in @N, and find the maximum. +# +my $max = max my @N = <> =~ /[0-9]+/g; + +my $w = length $max; # Width of the largest number, so we + # we can align things properly. + +# +# Print histograms. We're counting down from the maximum volume to 1, +# and for each index, if the volume on given index equals, or exceeds +# the current volume, we print a #, else, we print a space. +# +for (my $volume = $max; $volume; $volume --) { + printf "%${w}d" => $volume; + printf " %${w}s" => $N [$_] >= $volume ? "#" : " " for keys @N; + print "\n"; +} + +# +# Print the line with bars. +# +my $bar = "_" x $w; +print $bar, " $bar" x @N, "\n"; + +# +# Print the sums (just the values in @N) +# +print " " x $w; +printf " %${w}d" => $_ for @N; +print "\n"; + + +__END__ |
