From 5c75ba06822ed08cc222f73636fe7bc72eef5fa3 Mon Sep 17 00:00:00 2001 From: "Markus \"Holli\" Holzer" Date: Wed, 23 Sep 2020 20:52:20 +0200 Subject: now with histogram --- challenge-079/markus-holzer/raku/ch-2.raku | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/challenge-079/markus-holzer/raku/ch-2.raku b/challenge-079/markus-holzer/raku/ch-2.raku index d674dca8af..c3a961e708 100644 --- a/challenge-079/markus-holzer/raku/ch-2.raku +++ b/challenge-079/markus-holzer/raku/ch-2.raku @@ -1,9 +1,26 @@ unit sub MAIN( *@N where @N.all ~~ Int ); +my $max = @N.max; + +my @histo = reverse [Z] + (' ', '-', |(1..$max) ), + |@N.map: { $_, '-', |('#' xx $_), |(' ' xx ($max - $_)) }; + +my @pretty = @histo.map({ + .join('') + .subst(/ ( \s+ ) /, { '~' x .chars }, :g) + .subst('', ' ', :g )}); + + +.say and .indices('~').elems.say with @pretty.join("\n"); + +# This was my first attempt before I realized we're supposed to +# print the histogram too and I can just count the ~ in the output + sub index-find { @N.pairs.grep( *.value >= $^h ).map: *.key } sub index-diff { ($^i.cache.skip >>->> $^i).map: * - 1 } -say (@N.max...0) +say ($max...0) .map( &index-find ) .map( &index-diff ) .flat -- cgit