aboutsummaryrefslogtreecommitdiff
path: root/challenge-079
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-21 22:14:53 +0100
committerGitHub <noreply@github.com>2020-09-21 22:14:53 +0100
commitf37c0ef2beea77ef16d7ef6dc1ab7fb01f5f3c66 (patch)
treef5eb45bfcf74311561e1383e805d7b5f1959414c /challenge-079
parent1d14ced6b6ce2dd1bfc09326f43411c8d779b255 (diff)
parent6a9cf80ad7714090fcdf2d389f2ba4c4046b46a7 (diff)
downloadperlweeklychallenge-club-f37c0ef2beea77ef16d7ef6dc1ab7fb01f5f3c66.tar.gz
perlweeklychallenge-club-f37c0ef2beea77ef16d7ef6dc1ab7fb01f5f3c66.tar.bz2
perlweeklychallenge-club-f37c0ef2beea77ef16d7ef6dc1ab7fb01f5f3c66.zip
Merge pull request #2338 from holli-holzer/master
initial
Diffstat (limited to 'challenge-079')
-rw-r--r--challenge-079/markus-holzer/raku/bench.raku32
-rw-r--r--challenge-079/markus-holzer/raku/ch-1.raku7
-rw-r--r--challenge-079/markus-holzer/raku/ch-2.raku10
3 files changed, 49 insertions, 0 deletions
diff --git a/challenge-079/markus-holzer/raku/bench.raku b/challenge-079/markus-holzer/raku/bench.raku
new file mode 100644
index 0000000000..1ab34ceea2
--- /dev/null
+++ b/challenge-079/markus-holzer/raku/bench.raku
@@ -0,0 +1,32 @@
+use Bench;
+
+unit sub MAIN(Int $N = 42);
+
+#say i($N);
+#say r($N);
+#say f($N);
+#say k($N);
+#say l($N);
+
+sub i {
+ +$^n.base(2).indices(1) }
+
+sub r {
+ +($^n.base(2) ~~ m:g/1/) }
+
+sub f {
+ $^n == 0 ?? 0 !! $^n !%% 2 + f( $^n div 2 ) }
+
+sub k($n is copy) {
+ my $c = 0; while $n != 0 { $n = $n +& ($n-1); $c++ }; $c }
+
+sub l {
+ state @b = 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4;
+ $^n == 0 ?? 0 !! @b[ $^n +& 0xf ] + l($^n +> 4) }
+
+Bench.new.timethese( 10000, {
+ base2-with-indices => { i($N) },
+ base2-with-regex => { r($N) },
+ div2-recursive => { f($N) },
+ kernighan => { k($N) },
+ lookup-recursive => { l($N) }}) \ No newline at end of file
diff --git a/challenge-079/markus-holzer/raku/ch-1.raku b/challenge-079/markus-holzer/raku/ch-1.raku
new file mode 100644
index 0000000000..592d814445
--- /dev/null
+++ b/challenge-079/markus-holzer/raku/ch-1.raku
@@ -0,0 +1,7 @@
+unit sub MAIN( Int $N );
+
+# This is not only the simplest, but also a quite fast solution
+# It only loses (sometimes) to the kernighan algorithm
+# See bench.raku in this directory
+
+say ($N...1).map( + *.base(2).indices(1) ).sum % 1000000007 \ No newline at end of file
diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku
new file mode 100644
index 0000000000..d674dca8af
--- /dev/null
+++ b/challenge-079/markus-holzer/raku/ch-2.raku
@@ -0,0 +1,10 @@
+unit sub MAIN( *@N where @N.all ~~ Int );
+
+sub index-find { @N.pairs.grep( *.value >= $^h ).map: *.key }
+sub index-diff { ($^i.cache.skip >>->> $^i).map: * - 1 }
+
+say (@N.max...0)
+ .map( &index-find )
+ .map( &index-diff )
+ .flat
+ .sum; \ No newline at end of file