aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-21 21:55:54 +0100
committerGitHub <noreply@github.com>2020-09-21 21:55:54 +0100
commit6fb419cb6d1929fa5cf3be8e00ebac54b381daf8 (patch)
tree4a63386502c8e8f95e4b51464d8b04740114d086
parent8c12c4557b80991e4ab01bfefb2b22674a205bb7 (diff)
parentcd4e516ab22103d20e3c4002129a71b7fc9c0dd0 (diff)
downloadperlweeklychallenge-club-6fb419cb6d1929fa5cf3be8e00ebac54b381daf8.tar.gz
perlweeklychallenge-club-6fb419cb6d1929fa5cf3be8e00ebac54b381daf8.tar.bz2
perlweeklychallenge-club-6fb419cb6d1929fa5cf3be8e00ebac54b381daf8.zip
Merge pull request #2335 from Scimon/master
This weeks challenges
-rw-r--r--challenge-079/simon-proctor/raku/ch-1.raku10
-rw-r--r--challenge-079/simon-proctor/raku/ch-2.raku63
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-079/simon-proctor/raku/ch-1.raku b/challenge-079/simon-proctor/raku/ch-1.raku
new file mode 100644
index 0000000000..4555876047
--- /dev/null
+++ b/challenge-079/simon-proctor/raku/ch-1.raku
@@ -0,0 +1,10 @@
+#!/usr/bin/env raku
+
+use v6;
+
+#| Count the total numbrer of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007
+sub MAIN (
+ UInt $N #= Max number to count up to
+) {
+ say ( [+] (1..$N).race.map( *.base(2).comb ).flat ) % 1000000007
+}
diff --git a/challenge-079/simon-proctor/raku/ch-2.raku b/challenge-079/simon-proctor/raku/ch-2.raku
new file mode 100644
index 0000000000..dc65ae171b
--- /dev/null
+++ b/challenge-079/simon-proctor/raku/ch-2.raku
@@ -0,0 +1,63 @@
+#!/usr/bin/env raku
+
+use v6;
+
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+#| Given a list of height find the area of the largest single rectangle
+sub MAIN (
+ *@heights where { $_.all ~~ UInt && $_.elems >= 1 } , #= List of height
+) {
+ my @rain-levels = calculate-rain-levels( @heights );
+ draw-heights( @heights, @rain-levels );
+ say [+] @rain-levels.map(*.area);
+}
+
+class RainArea {
+ has Range $.range;
+ has UInt $.height;
+ has UInt $.area;
+}
+
+sub draw-heights( @heights, @rain ) {
+ my $max = @heights.max;
+ my $width = $max.codes;
+ my $bar = '#' x $width;
+ my $spc = ' ' x $width;
+ my $dsh = '-' x $width;
+ my $wtr = '~' x $width;
+ my &spr = -> $x { sprintf( "%{$width}d", $x ) }
+ my &prt = -> $h {
+ -> $k, $v {
+ my @found = @rain.grep( -> $r { $k ~~ $r.range } );
+ if (@found && $h <= @found[0].height) {
+ $v >= $h ?? $bar !! $wtr;
+ } else {
+ $v >= $h ?? $bar !! $spc;
+ }
+ };
+ };
+
+ for $max...1 -> $h {
+ my &fnc = &prt($h);
+ ( &spr( $h ), |@heights.kv.map( &fnc ) ).join(" ").say;
+ }
+ ( $dsh xx @heights.elems + 1 ).join(" ").say;
+ ( $spc, |@heights.map( &spr ) ).join(" ").say;
+}
+
+multi sub calculate-rain-levels( @heights where { any(@heights[0^..^*-1]) > any(@heights[0],@heights[*-1]) }, $offset = 0) {
+ my $max = max(@heights[0^..^*-1]);
+ my $mid-idx = @heights[0^..^*-1].kv.map( -> $k, $v { $v == $max ?? $k+1 !! Empty } )[0];
+
+ return ( |calculate-rain-levels( @heights[0..$mid-idx],$offset ), |calculate-rain-levels( @heights[$mid-idx..*],$mid-idx+$offset ) )
+}
+
+multi sub calculate-rain-levels( @heights where { @heights.elems > 2 }, $offset=0 ) {
+ my $height = min( @heights[0], @heights[*-1] );
+ my $area = $height * ( @heights.elems - 2 );
+ $area -= [+] @heights[0^..^*-1];
+ return RainArea.new( range => ( $offset..^($offset+@heights.elems) ), :$area, :$height );
+}
+
+multi sub calculate-rain-levels(@,$) { return Empty}