From 035364d2ca9ca93bc276349c47fbd1fc4fb1e683 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 21 Sep 2020 09:21:22 +0100 Subject: Challenge 1 --- challenge-079/simon-proctor/raku/ch-1.raku | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 challenge-079/simon-proctor/raku/ch-1.raku 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 +} -- cgit From 6c3470c8dab91bd9c3bfdc64c509e701ab798eb1 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 21 Sep 2020 11:15:23 +0100 Subject: Challenge 2 done --- challenge-079/simon-proctor/raku/ch-2.raku | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 challenge-079/simon-proctor/raku/ch-2.raku 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..9824854e3a --- /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(@,$) {} -- cgit From cd4e516ab22103d20e3c4002129a71b7fc9c0dd0 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 21 Sep 2020 11:44:59 +0100 Subject: Small fix --- challenge-079/simon-proctor/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-079/simon-proctor/raku/ch-2.raku b/challenge-079/simon-proctor/raku/ch-2.raku index 9824854e3a..dc65ae171b 100644 --- a/challenge-079/simon-proctor/raku/ch-2.raku +++ b/challenge-079/simon-proctor/raku/ch-2.raku @@ -60,4 +60,4 @@ multi sub calculate-rain-levels( @heights where { @heights.elems > 2 }, $offset= return RainArea.new( range => ( $offset..^($offset+@heights.elems) ), :$area, :$height ); } -multi sub calculate-rain-levels(@,$) {} +multi sub calculate-rain-levels(@,$) { return Empty} -- cgit