diff options
| author | boblied <boblied@gmail.com> | 2020-09-24 11:59:23 -0500 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2020-09-24 11:59:23 -0500 |
| commit | 0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74 (patch) | |
| tree | 425334fde2f16238f4557a9bbe43ac384f15350f /challenge-079/bob-lied/perl/lib | |
| parent | 6b075dd9c022007fd4711c2401f4f5bee6b68a6e (diff) | |
| download | perlweeklychallenge-club-0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74.tar.gz perlweeklychallenge-club-0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74.tar.bz2 perlweeklychallenge-club-0205bba3b8a2eb1079bb3cc34b06aa16d8c81d74.zip | |
Solutions for PWC 079
Diffstat (limited to 'challenge-079/bob-lied/perl/lib')
| -rw-r--r-- | challenge-079/bob-lied/perl/lib/CountSetBit.pm | 22 | ||||
| -rw-r--r-- | challenge-079/bob-lied/perl/lib/TrappedRainWater.pm | 77 |
2 files changed, 93 insertions, 6 deletions
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; |
