aboutsummaryrefslogtreecommitdiff
path: root/challenge-079
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-079')
-rw-r--r--challenge-079/dave-cross/perl/ch-1.pl24
-rw-r--r--challenge-079/dave-cross/perl/ch-2.pl52
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-079/dave-cross/perl/ch-1.pl b/challenge-079/dave-cross/perl/ch-1.pl
new file mode 100644
index 0000000000..c137165bb2
--- /dev/null
+++ b/challenge-079/dave-cross/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature qw[say signatures];
+no warnings 'experimental::signatures';
+
+if (!@ARGV or !$ARGV[0] or $ARGV[0] =~ /\D/) {
+ die "Please give me a positive integer\n";
+}
+
+my $n = shift;
+
+my $count;
+
+for (1 .. $n) {
+ $count += set_bits_in($_);
+}
+
+say "$count % 1000000007 = $count";
+
+sub set_bits_in($decimal) {
+ sprintf('%b', $decimal) =~ tr/1/1/;
+}
diff --git a/challenge-079/dave-cross/perl/ch-2.pl b/challenge-079/dave-cross/perl/ch-2.pl
new file mode 100644
index 0000000000..dc1fb23122
--- /dev/null
+++ b/challenge-079/dave-cross/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature qw[say signatures];
+no warnings 'experimental::signatures';
+
+use List::Util qw[min max];
+
+if (!@ARGV or grep { ! $_ or /\D/ } @ARGV) {
+ die "Give me a list of positive integers";
+}
+
+say plot_hist(@ARGV);
+
+say count_water(@ARGV);
+
+sub plot_hist(@data) {
+ my $max = max @data;
+ my $hist = "\n";
+
+ for (1 .. $max) {
+ my $level = $max - $_ + 1;
+ $hist .= $level;
+ $hist .= +($_ >= $level) ? ' #' : ' ' for @data;
+ $hist .= "\n";
+ }
+
+ $hist .= join ' ', ('-') x (@data + 1);
+ $hist .= "\n";
+ $hist .= join ' ', (' ', @data);
+ $hist .= "\n";
+
+ return $hist;
+}
+
+sub count_water(@data) {
+ my $water;
+
+ for (1 .. $#data - 1) {
+ $water += get_dip_size($_, @data);
+ }
+
+ return $water;
+}
+
+sub get_dip_size($idx, @data) {
+ my $l_max = max(@data[0 .. $idx]) - $data[$idx];
+ my $r_max = max(@data[$idx .. $#data]) - $data[$idx];
+
+ return min($l_max, $r_max);
+}