aboutsummaryrefslogtreecommitdiff
path: root/challenge-079
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-27 21:34:57 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-09-27 21:34:57 +0100
commitecabc8310923fdd156aa30af1501569c7c1bfad0 (patch)
tree3ae20130d256f26b613a2367b1cd099191bd006d /challenge-079
parent308bd1139ae9463a9f5a7d9e66cb600fe79d8b4f (diff)
downloadperlweeklychallenge-club-ecabc8310923fdd156aa30af1501569c7c1bfad0.tar.gz
perlweeklychallenge-club-ecabc8310923fdd156aa30af1501569c7c1bfad0.tar.bz2
perlweeklychallenge-club-ecabc8310923fdd156aa30af1501569c7c1bfad0.zip
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-079')
-rw-r--r--challenge-079/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-079/arne-sommer/perl/ch-1.pl10
-rwxr-xr-xchallenge-079/arne-sommer/perl/count-set-bits-perl10
-rwxr-xr-xchallenge-079/arne-sommer/raku/ch-1.raku7
-rwxr-xr-xchallenge-079/arne-sommer/raku/ch-2.raku115
-rwxr-xr-xchallenge-079/arne-sommer/raku/count-set-bits7
-rwxr-xr-xchallenge-079/arne-sommer/raku/count-set-bits-recursive16
-rwxr-xr-xchallenge-079/arne-sommer/raku/count-set-bits-sequence18
-rwxr-xr-xchallenge-079/arne-sommer/raku/count-set-bits-sequence-upto20
-rwxr-xr-xchallenge-079/arne-sommer/raku/trapped-rain-water115
-rwxr-xr-xchallenge-079/arne-sommer/raku/trapped-rain-water-plain38
11 files changed, 357 insertions, 0 deletions
diff --git a/challenge-079/arne-sommer/blog.txt b/challenge-079/arne-sommer/blog.txt
new file mode 100644
index 0000000000..902a15fe7e
--- /dev/null
+++ b/challenge-079/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/counting-water.html
diff --git a/challenge-079/arne-sommer/perl/ch-1.pl b/challenge-079/arne-sommer/perl/ch-1.pl
new file mode 100755
index 0000000000..e4e0fef7c7
--- /dev/null
+++ b/challenge-079/arne-sommer/perl/ch-1.pl
@@ -0,0 +1,10 @@
+#! /usr/bin/env perl
+
+use feature 'say';
+use List::Util qw/sum/;
+
+my $N = $ARGV[0] // die 'Missing value for $N';
+
+die '$N must be a positive integer' unless $N > 0 && int($N) == $N;
+
+say sum(split("", join("", map { sprintf('%b', $_) } 1 .. $N))) % 1000000007;
diff --git a/challenge-079/arne-sommer/perl/count-set-bits-perl b/challenge-079/arne-sommer/perl/count-set-bits-perl
new file mode 100755
index 0000000000..e4e0fef7c7
--- /dev/null
+++ b/challenge-079/arne-sommer/perl/count-set-bits-perl
@@ -0,0 +1,10 @@
+#! /usr/bin/env perl
+
+use feature 'say';
+use List::Util qw/sum/;
+
+my $N = $ARGV[0] // die 'Missing value for $N';
+
+die '$N must be a positive integer' unless $N > 0 && int($N) == $N;
+
+say sum(split("", join("", map { sprintf('%b', $_) } 1 .. $N))) % 1000000007;
diff --git a/challenge-079/arne-sommer/raku/ch-1.raku b/challenge-079/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..4eeee12e88
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $N where $N > 0);
+
+say (1..$N).map({ $_.fmt('%b') })>>.comb.flat.sum % 1000000007;
+
+
diff --git a/challenge-079/arne-sommer/raku/ch-2.raku b/challenge-079/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..2abc1d36a5
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,115 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int && all(@N) > 0, :v(:$verbose), :s(:$show), :h(:$html));
+
+if @N.elems == 1|2
+{
+ say 0;
+ exit;
+}
+
+my $col-blue = "\e[44m";
+my $col-green = "\e[42m";
+my $col-red = "\e[101m";
+my $col-stop = "\e[0m";
+my $col-swap = "";
+
+if ($html)
+{
+ $col-blue = '<span class="text-light bg-primary">';
+ $col-green = '<span class="text-light bg-success">';
+ $col-red = '<span class="text-light bg-danger">';
+ $col-stop = '</span>';
+ $col-swap = '</span>';
+}
+
+my $old-sum = @N.sum;
+my $elems = @N.elems;
+my @N-new = @N;
+
+say ": Old sum: $old-sum" if $verbose;
+
+for 0 .. $elems -3 -> $left
+{
+ for $elems -1 ... $left +2 -> $right
+ {
+ show-sliding-window($left, $right, $col-green, "Before filling", @N-new) if $verbose;
+
+ my @old = @N-new;
+
+ my $L = @N-new[$left];
+ my $R = @N-new[$right];
+ my @A = @N-new[$left+1 .. $right-1];
+
+ my $min = min($L, $R);
+
+ if $min > any (@A)
+ {
+ for $left+1 .. $right-1 -> $index
+ {
+ @N-new[$index] = $min if @N-new[$index] < $min;
+ }
+ }
+ show-sliding-window($left, $right, $col-blue, "After filling ", @old) if $verbose;
+ }
+}
+
+my $new-sum = @N-new.sum;
+
+say ": New sum: $new-sum" if $verbose;
+
+show-histogram if $show;
+
+say $new-sum - $old-sum;
+
+sub show-sliding-window($left, $right, $col, $label, @old)
+{
+ print ": $label: ";
+
+ for ^@N -> $index
+ {
+ if $index < $left || $index > $right
+ {
+ print @N-new[$index] ~ " ";
+ }
+ elsif @N-new[$index] == @old[$index]
+ {
+ print $col ~ @N-new[$index] ~ " " ~ $col-stop;
+ }
+ else
+ {
+ print $col-red ~ @N-new[$index] ~ " " ~ $col-stop;
+ }
+ }
+ print "\n";
+}
+
+sub show-histogram
+{
+ my $rows = @N.max;
+ my $cols = @N.elems;
+
+ for $rows ... 1 -> $row
+ {
+ print ": $row ";
+ for 1 .. $cols -> $col
+ {
+ if @N[$col-1] >= $row
+ {
+ print "# ";
+ }
+ elsif @N-new[$col-1] >= $row
+ {
+ print $col-red ~ "#" ~ $col-stop ~ " ";
+ }
+ else
+ {
+ print " ";
+ }
+ }
+ print "\n";
+ }
+ say ": " ~ "-" x $cols + $cols + 2;
+ say ": ", @N.join(" "), " (before)";
+ say ": ", @N-new.join(" "), " (after)";
+} \ No newline at end of file
diff --git a/challenge-079/arne-sommer/raku/count-set-bits b/challenge-079/arne-sommer/raku/count-set-bits
new file mode 100755
index 0000000000..4eeee12e88
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/count-set-bits
@@ -0,0 +1,7 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $N where $N > 0);
+
+say (1..$N).map({ $_.fmt('%b') })>>.comb.flat.sum % 1000000007;
+
+
diff --git a/challenge-079/arne-sommer/raku/count-set-bits-recursive b/challenge-079/arne-sommer/raku/count-set-bits-recursive
new file mode 100755
index 0000000000..0b7e93dfb3
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/count-set-bits-recursive
@@ -0,0 +1,16 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $N where $N > 0);
+
+say cbs($N) % 1000000007;
+
+multi sub cbs (Int $N)
+{
+ return $N.fmt('%b')>>.comb.flat.sum + cbs($N-1);
+}
+
+multi sub cbs (1)
+{
+ return 1;
+}
+
diff --git a/challenge-079/arne-sommer/raku/count-set-bits-sequence b/challenge-079/arne-sommer/raku/count-set-bits-sequence
new file mode 100755
index 0000000000..1692fbe5cb
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/count-set-bits-sequence
@@ -0,0 +1,18 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $N where $N > 0);
+
+my $csb := gather
+{
+ take "";
+ take 1;
+ state $sum = 1;
+ state $count = 1;
+ loop
+ {
+ $sum += (++$count).fmt('%b')>>.comb.flat.sum;
+ take $sum;
+ }
+}
+
+say $csb[$N] % 1000000007;
diff --git a/challenge-079/arne-sommer/raku/count-set-bits-sequence-upto b/challenge-079/arne-sommer/raku/count-set-bits-sequence-upto
new file mode 100755
index 0000000000..9f1d9d5aff
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/count-set-bits-sequence-upto
@@ -0,0 +1,20 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $N where $N > 0, :u(:$upto));
+
+my $csb := gather
+{
+ take "";
+ take 1;
+ state $sum = 1;
+ state $count = 1;
+ loop
+ {
+ $sum += (++$count).fmt('%b')>>.comb.flat.sum;
+ take $sum;
+ }
+}
+
+$upto
+ ?? ( say "$_ (binary { $_.fmt('%b') }): { $csb[$_] % 1000000007 }" for 1 .. $N )
+ !! say $csb[$N];
diff --git a/challenge-079/arne-sommer/raku/trapped-rain-water b/challenge-079/arne-sommer/raku/trapped-rain-water
new file mode 100755
index 0000000000..2abc1d36a5
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/trapped-rain-water
@@ -0,0 +1,115 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int && all(@N) > 0, :v(:$verbose), :s(:$show), :h(:$html));
+
+if @N.elems == 1|2
+{
+ say 0;
+ exit;
+}
+
+my $col-blue = "\e[44m";
+my $col-green = "\e[42m";
+my $col-red = "\e[101m";
+my $col-stop = "\e[0m";
+my $col-swap = "";
+
+if ($html)
+{
+ $col-blue = '<span class="text-light bg-primary">';
+ $col-green = '<span class="text-light bg-success">';
+ $col-red = '<span class="text-light bg-danger">';
+ $col-stop = '</span>';
+ $col-swap = '</span>';
+}
+
+my $old-sum = @N.sum;
+my $elems = @N.elems;
+my @N-new = @N;
+
+say ": Old sum: $old-sum" if $verbose;
+
+for 0 .. $elems -3 -> $left
+{
+ for $elems -1 ... $left +2 -> $right
+ {
+ show-sliding-window($left, $right, $col-green, "Before filling", @N-new) if $verbose;
+
+ my @old = @N-new;
+
+ my $L = @N-new[$left];
+ my $R = @N-new[$right];
+ my @A = @N-new[$left+1 .. $right-1];
+
+ my $min = min($L, $R);
+
+ if $min > any (@A)
+ {
+ for $left+1 .. $right-1 -> $index
+ {
+ @N-new[$index] = $min if @N-new[$index] < $min;
+ }
+ }
+ show-sliding-window($left, $right, $col-blue, "After filling ", @old) if $verbose;
+ }
+}
+
+my $new-sum = @N-new.sum;
+
+say ": New sum: $new-sum" if $verbose;
+
+show-histogram if $show;
+
+say $new-sum - $old-sum;
+
+sub show-sliding-window($left, $right, $col, $label, @old)
+{
+ print ": $label: ";
+
+ for ^@N -> $index
+ {
+ if $index < $left || $index > $right
+ {
+ print @N-new[$index] ~ " ";
+ }
+ elsif @N-new[$index] == @old[$index]
+ {
+ print $col ~ @N-new[$index] ~ " " ~ $col-stop;
+ }
+ else
+ {
+ print $col-red ~ @N-new[$index] ~ " " ~ $col-stop;
+ }
+ }
+ print "\n";
+}
+
+sub show-histogram
+{
+ my $rows = @N.max;
+ my $cols = @N.elems;
+
+ for $rows ... 1 -> $row
+ {
+ print ": $row ";
+ for 1 .. $cols -> $col
+ {
+ if @N[$col-1] >= $row
+ {
+ print "# ";
+ }
+ elsif @N-new[$col-1] >= $row
+ {
+ print $col-red ~ "#" ~ $col-stop ~ " ";
+ }
+ else
+ {
+ print " ";
+ }
+ }
+ print "\n";
+ }
+ say ": " ~ "-" x $cols + $cols + 2;
+ say ": ", @N.join(" "), " (before)";
+ say ": ", @N-new.join(" "), " (after)";
+} \ No newline at end of file
diff --git a/challenge-079/arne-sommer/raku/trapped-rain-water-plain b/challenge-079/arne-sommer/raku/trapped-rain-water-plain
new file mode 100755
index 0000000000..b3aa1a6870
--- /dev/null
+++ b/challenge-079/arne-sommer/raku/trapped-rain-water-plain
@@ -0,0 +1,38 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@N where @N.elems > 0 && all(@N) ~~ Int && all(@N) > 0);
+
+if @N.elems == 1|2
+{
+ say 0;
+ exit;
+}
+
+my $old-sum = @N.sum;
+my $elems = @N.elems;
+my @N-new = @N;
+
+for 0 .. $elems -3 -> $left
+{
+ for $elems -1 ... $left +2 -> $right
+ {
+ my $L = @N-new[$left];
+ my $R = @N-new[$right];
+ my @A = @N-new[$left+1 .. $right-1];
+
+ my $min = min($L, $R);
+
+ if $min > any (@A)
+ {
+ for $left+1 .. $right-1 -> $index
+ {
+ @N-new[$index] = $min if @N-new[$index] < $min;
+ }
+ }
+ }
+}
+
+my $new-sum = @N-new.sum;
+
+say $new-sum - $old-sum;
+