aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/bob-lied
diff options
context:
space:
mode:
authorboblied <boblied@gmail.com>2020-09-24 11:59:23 -0500
committerboblied <boblied@gmail.com>2020-09-24 11:59:23 -0500
commit0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74 (patch)
tree425334fde2f16238f4557a9bbe43ac384f15350f /challenge-079/bob-lied
parent6b075dd9c022007fd4711c2401f4f5bee6b68a6e (diff)
downloadperlweeklychallenge-club-0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74.tar.gz
perlweeklychallenge-club-0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74.tar.bz2
perlweeklychallenge-club-0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74.zip
Solutions for PWC 079
Diffstat (limited to 'challenge-079/bob-lied')
-rwxr-xr-xchallenge-079/bob-lied/perl/ch-1.pl10
-rwxr-xr-xchallenge-079/bob-lied/perl/ch-2.pl22
-rw-r--r--challenge-079/bob-lied/perl/lib/CountSetBit.pm22
-rw-r--r--challenge-079/bob-lied/perl/lib/TrappedRainWater.pm77
-rw-r--r--challenge-079/bob-lied/perl/t/CountSetBit.t21
-rw-r--r--challenge-079/bob-lied/perl/t/TrappedRainWater.t5
6 files changed, 137 insertions, 20 deletions
diff --git a/challenge-079/bob-lied/perl/ch-1.pl b/challenge-079/bob-lied/perl/ch-1.pl
index 94893c07d4..ba9a661621 100755
--- a/challenge-079/bob-lied/perl/ch-1.pl
+++ b/challenge-079/bob-lied/perl/ch-1.pl
@@ -30,17 +30,15 @@ use Getopt::Long;
use lib "lib";
use CountSetBit;
-sub Usage { "Usage: $0 args" };
+sub Usage { "Usage: $0 N \t\t# N > 0" };
my $Verbose = 0;
GetOptions('verbose' => \$Verbose);
-my $arg = shift;
-my @list = @ARGV;
+my $N = shift;
-die Usage() unless $arg;
-die Usage() unless @list;
+die Usage() unless $N && $N >= 0;
-my $task = CountSetBit->new();
+my $task = CountSetBit->new($N);
my $result = $task->run();
say $result;
diff --git a/challenge-079/bob-lied/perl/ch-2.pl b/challenge-079/bob-lied/perl/ch-2.pl
index 6eafe88497..55500582d1 100755
--- a/challenge-079/bob-lied/perl/ch-2.pl
+++ b/challenge-079/bob-lied/perl/ch-2.pl
@@ -38,19 +38,25 @@ no warnings qw/ experimental::signatures /;
use Getopt::Long;
use lib "lib";
-use Task2;
+use TrappedRainWater;
-sub Usage { "Usage: $0 args" };
+sub Usage { "Usage: $0 list-of-int-gt-0" };
my $Verbose = 0;
GetOptions('verbose' => \$Verbose);
-my $arg = shift;
-my @list = @ARGV;
+my $list = "@ARGV";
-die Usage() unless $arg;
-die Usage() unless @list;
+die Usage() unless $list;
+
+$list =~ s/[(),]/ /g;
+my @list = split(" ", $list);
+
+die Usage() unless grep /\d+/, @list;
+
+my $task = TrappedRainWater->new(\@list);
+$task->show() if $Verbose;
-my $task = Task2->new();
my $result = $task->run();
-say $result;
+$task->show() if $Verbose;
+say "-----\n", $result;
diff --git a/challenge-079/bob-lied/perl/lib/CountSetBit.pm b/challenge-079/bob-lied/perl/lib/CountSetBit.pm
index 37f74ea838..1733d4e676 100644
--- a/challenge-079/bob-lied/perl/lib/CountSetBit.pm
+++ b/challenge-079/bob-lied/perl/lib/CountSetBit.pm
@@ -21,19 +21,35 @@ our @ISA = qw(Exporter);
our @EXPORT = qw();
our @EXPORT_OK = qw();
-sub new($class, $name1)
+sub new($class, $n)
{
$class = ref($class) || $class;
my $self = {
- _name1 => $name1,
+ _n => $n,
+
+ _sum => 0,
};
bless $self, $class;
return $self;
}
+# https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/
sub run($self)
{
- return undef;
+ $self->{_sum} += $self->_bitsOf($_) for ( 1 .. $self->{_n} );
+ return ( $self->{_sum} % 1000000007 );
+}
+
+sub _bitsOf($self, $n)a
+{
+ my $count = 0;
+
+ while ( $n > 0 )
+ {
+ $count++;
+ $n = $n & ($n-1);
+ }
+ return $count;
}
1;
diff --git a/challenge-079/bob-lied/perl/lib/TrappedRainWater.pm b/challenge-079/bob-lied/perl/lib/TrappedRainWater.pm
index cb98286764..34285e52b9 100644
--- a/challenge-079/bob-lied/perl/lib/TrappedRainWater.pm
+++ b/challenge-079/bob-lied/perl/lib/TrappedRainWater.pm
@@ -16,24 +16,95 @@ use v5.30;
use feature qw/ signatures /;
no warnings qw/ experimental::signatures /;
+use List::Util qw/ max /;
+
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw();
our @EXPORT_OK = qw();
-sub new($class, $name1)
+sub new($class, $listref)
{
$class = ref($class) || $class;
my $self = {
- _name1 => $name1,
+ _list => $listref,
+ _numCol => scalar(@$listref),
+ _numRow => List::Util::max(@$listref),
+
+ _grid => [],
+ _trap => [],
};
bless $self, $class;
+
+ $self->_setup();
return $self;
}
+sub _setup($self)
+{
+ my ($lst, $trap, $g, $numRow, $numCol) = @{$self}{ qw(_list _trap _grid _numRow _numCol) };
+
+ for ( my $r = 0; $r < $numRow ; $r++ )
+ {
+ for ( my $c = 0; $c < $numCol; $c++ )
+ {
+ $g->[$r][$c] = ( $lst->[$c] > $r ? '#' : ' ');
+ $trap->[$r][$c] = 0;
+ }
+ }
+}
+
+sub show($self)
+{
+ my ($g, $numRow, $numCol) = @{$self}{ qw(_trap _numRow _numCol) };
+ print " ";
+ for ( my $c = 0 ; $c < $numCol; $c++ )
+ {
+ print "[$c]";
+ }
+ print "\n";
+ for ( my $r = $numRow - 1; $r >= 0 ; $r-- )
+ {
+ print "[$r] ";
+ say " ", join(' ', @{$g->[$r]}), " ";
+ }
+
+}
+
+sub _hasWall($self)
+{
+ my $sawWall = 0;
+ my $g = $self->{_grid};
+ my $trap = $self->{_trap};
+
+ for (my $r = 0 ; $r < $self->{_numRow} ; $r++ )
+ {
+ # Increment count if there's a wall on the left
+ for (my $c = 0 ; $c < $self->{_numCol} ; $c++ )
+ {
+ if ( $g->[$r][$c] eq '#' ) { $sawWall = 1 }
+ elsif ( $sawWall ) { $trap->[$r][$c]++ }
+ }
+ # Increment count if there's a wall on the right
+ $sawWall = 0;
+ for (my $c = $self->{_numCol}-1 ; $c >= 0 ; $c-- )
+ {
+ if ( $g->[$r][$c] eq '#' ) { $sawWall = 1 }
+ elsif ( $sawWall ) { $trap->[$r][$c]++ }
+ }
+ $sawWall = 0;
+ }
+ # Assume there's a wall on the bottom (all list values are > 0).
+}
+
sub run($self)
{
- return undef;
+ $self->_hasWall();
+
+ my @flattrap = map { ref eq 'ARRAY' ? @$_ : $_ } @{$self->{_trap}};
+
+ my $holdsWater = grep { $_ == 2 } @flattrap;
+ return $holdsWater;
}
1;
diff --git a/challenge-079/bob-lied/perl/t/CountSetBit.t b/challenge-079/bob-lied/perl/t/CountSetBit.t
index ff4062688b..8652b033d5 100644
--- a/challenge-079/bob-lied/perl/t/CountSetBit.t
+++ b/challenge-079/bob-lied/perl/t/CountSetBit.t
@@ -10,5 +10,26 @@ use warnings;
use v5.30;
use Test2::V0;
+use CountSetBit;
+
+my $csb = CountSetBit->new(4);
+isa_ok( $csb, [ qw(CountSetBit) ], "Constructor" );
+
+is( CountSetBit->new( 1)->run(), 1, "1");
+is( CountSetBit->new( 2)->run(), 2, "2");
+is( CountSetBit->new( 3)->run(), 4, "3");
+is( CountSetBit->new( 4)->run(), 5, "4");
+is( CountSetBit->new( 5)->run(), 7, "5");
+is( CountSetBit->new( 6)->run(), 9, "6");
+is( CountSetBit->new( 7)->run(), 12, "7");
+is( CountSetBit->new( 8)->run(), 13, "8");
+is( CountSetBit->new( 9)->run(), 15, "8");
+is( CountSetBit->new(10)->run(), 17, "8");
+is( CountSetBit->new(11)->run(), 20, "8");
+is( CountSetBit->new(12)->run(), 22, "8");
+is( CountSetBit->new(13)->run(), 25, "8");
+is( CountSetBit->new(14)->run(), 28, "8");
+is( CountSetBit->new(15)->run(), 32, "8");
+is( CountSetBit->new(16)->run(), 33, "8");
done_testing();
diff --git a/challenge-079/bob-lied/perl/t/TrappedRainWater.t b/challenge-079/bob-lied/perl/t/TrappedRainWater.t
index b40e30679e..c3d419b298 100644
--- a/challenge-079/bob-lied/perl/t/TrappedRainWater.t
+++ b/challenge-079/bob-lied/perl/t/TrappedRainWater.t
@@ -10,5 +10,10 @@ use warnings;
use v5.30;
use Test2::V0;
+use TrappedRainWater;
+
+my $trw = TrappedRainWater->new(2, 1, 4, 1, 2, 5);
+
+isa_ok( $trw, [ qw(TrappedRainWater) ], "Constructor" );
done_testing();