diff options
| author | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-08-26 09:03:28 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zoopla.co.uk> | 2020-08-26 09:03:28 +0100 |
| commit | 2e8749976783f09a56c3249c7034cd157431cc0f (patch) | |
| tree | 647cc2e6abce403535dfe1ffb5afb894e89222c9 | |
| parent | 8e487aa6a65d5e05e55bd8d17e819e70d025a7b9 (diff) | |
| download | perlweeklychallenge-club-2e8749976783f09a56c3249c7034cd157431cc0f.tar.gz perlweeklychallenge-club-2e8749976783f09a56c3249c7034cd157431cc0f.tar.bz2 perlweeklychallenge-club-2e8749976783f09a56c3249c7034cd157431cc0f.zip | |
Bring on the bonus challenges
| -rw-r--r-- | challenge-075/simon-proctor/raku/ch-2.raku | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/challenge-075/simon-proctor/raku/ch-2.raku b/challenge-075/simon-proctor/raku/ch-2.raku index 4497f86c7a..d081a8d119 100644 --- a/challenge-075/simon-proctor/raku/ch-2.raku +++ b/challenge-075/simon-proctor/raku/ch-2.raku @@ -9,20 +9,38 @@ sub MAIN ( Bool :g(:$graph-heights) = False, #=Draw the height histogram first *@heights where { $_.all ~~ UInt && $_.elems >= 1 } , #= List of height ) { - draw-heights( @heights ) if $graph-heights; - say calculate-max-rect( @heights ); + my $rect-details = calculate-max-rect( @heights ); + draw-heights( @heights, $rect-details ) if $graph-heights; + say $rect-details.area; } -sub draw-heights( @heights ) { +class RectDetails { + has Range $.range; + has UInt $.height; + has UInt $.area; +} + +sub draw-heights( @heights, $rect ) { my $max = @heights.max; my $width = $max.codes; my $bar = '#' x $width; my $spc = ' ' x $width; my $dsh = '-' x $width; + my $rct = 'X' x $width; my &spr = -> $x { sprintf( "%{$width}d", $x ) } + my &prt = -> $h { + -> $k, $v { + if ($k ~~ $rect.range) && ($h <= $rect.height) { + $v >= $h ?? $rct !! $spc; + } else { + $v >= $h ?? $bar !! $spc; + } + }; + }; for $max...1 -> $h { - ( &spr( $h ), |@heights.map( { $_ >= $h ?? $bar !! $spc } ) ).join(" ").say; + 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; @@ -31,13 +49,14 @@ sub draw-heights( @heights ) { sub calculate-max-rect( @heights ) { my $length = @heights.elems - 1; - my @ranges = ( (0..$length) X (0..$length) ).grep( { $_[0] <= $_[1] } ); + my @ranges = ( (0..$length) X.. (0..$length) ).grep( { $_.min <= $_.max } ); my $max = 0; + my $result = RectDetails.new( area => 0 ); - for @ranges -> ($s,$e) { - my $height = @heights[$s..$e].min; - my $area = $height * ($e - $s + 1); - $max = $area if $max < $area; + for @ranges -> $range { + my $height = @heights[$range.list].min; + my $area = $height * ($range.max - $range.min + 1); + $result = RectDetails.new( :$range, :$height, :$area ) if $result.area < $area; } - return $max; + return $result; } |
