aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-08-24 17:04:34 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-08-24 17:04:34 +0200
commit3b86361edc2bfef2c7e22711cc1955cebc965a90 (patch)
treebd20d5b60211f1da5df897b32fa24b9549de7d35
parent944b6d88bd4286f0458aff1709ee95fa1fae0299 (diff)
downloadperlweeklychallenge-club-3b86361edc2bfef2c7e22711cc1955cebc965a90.tar.gz
perlweeklychallenge-club-3b86361edc2bfef2c7e22711cc1955cebc965a90.tar.bz2
perlweeklychallenge-club-3b86361edc2bfef2c7e22711cc1955cebc965a90.zip
Bonus track graph.
Fixes also some problems.
-rw-r--r--challenge-075/luca-ferrari/raku/ch-2.p656
1 files changed, 51 insertions, 5 deletions
diff --git a/challenge-075/luca-ferrari/raku/ch-2.p6 b/challenge-075/luca-ferrari/raku/ch-2.p6
index 14cf0bef95..072ab8378a 100644
--- a/challenge-075/luca-ferrari/raku/ch-2.p6
+++ b/challenge-075/luca-ferrari/raku/ch-2.p6
@@ -12,8 +12,39 @@ class Rectangle {
has Int $.height;
has Int $.base;
+ has Range $.columns;
+
method area() { $!height * $!base }
- method Str() { "Rectangle with base $!base and height $!height ($!base x $!height)" }
+ method Str() { "Rectangle with base $!base and height $!height ($!base x $!height) {$!columns}" }
+}
+
+
+sub graph( Histogram :@histograms, Rectangle :$rectangle? ) {
+ my @lines;
+ my $max-height = max @histograms.map( { .height } );
+
+
+ while ( $max-height > 0 ) {
+ my @line;
+ @line.push: $max-height ~ '| ';
+
+ for @histograms {
+ my $column = .column;
+ my $height = .height;
+ my $to-print = $rectangle
+ && $column ~~ $rectangle.columns
+ && $rectangle.height >= $max-height
+ ?? ' X' !! ' #';
+ @line.push: .height >= $max-height ?? $to-print !! ' ';
+ }
+
+ @lines.push: @line.join;
+ $max-height--;
+ }
+
+ @lines.push: '---' x @histograms.elems;
+ @lines.push: ' ' ~ @histograms.map( { .column } ).join( ' ' );
+ @lines;
}
@@ -24,35 +55,50 @@ sub MAIN( *@A
my Histogram @histograms;
my $column = 1;
for @A {
- @histograms.push: Histogram.new: column => $column, height => $_.Int;
+ @histograms.push: Histogram.new: column => $column++, height => $_.Int;
}
my Rectangle @rectangles;
my ( $current-height, $current-width ) = Nil, Nil;
+ my ( $start-column, $end-column ) = Nil, Nil;
for 0 ..^ @histograms.elems -> $current-index {
( $current-height, $current-width ) = @histograms[ $current-index ].height, 0;
+ ( $start-column, $end-column ) = $current-index + 1 , $current-index + 1;
+
+ # go backwards to the first histogram that has a good height
+ my $starting-index = $current-index;
+ while ( $starting-index > 0 && @histograms[ $starting-index ].height >= $current-height ) {
+ $starting-index--;
+ }
- for 0 ..^ @histograms.elems {
+ for $starting-index ..^ @histograms.elems {
next if $_ < $current-index && @histograms[ $_ ].height < $current-height;
if @histograms[ $_ ].height < $current-height {
last;
}
else {
$current-width++;
+ $start-column = min $_ + 1 , $start-column;
+ $end-column = max $_ + 1, $end-column;
}
}
@rectangles.push: Rectangle.new( height => $current-height,
- base => $current-width );
+ base => $current-width,
+ columns => $start-column .. $end-column );
}
# get the first one with the biggest area
- @rectangles.sort( { $^b.area <=> $^a.area } ).first.put;
+ my Rectangle $biggest = @rectangles.sort( { $^b.area <=> $^a.area } ).first;
+ $biggest.put;
+ # try to print the histogram
+ "Following is the graph of the histogram".say;
+ graph( histograms => @histograms, rectangle => $biggest ).join( "\n" ).say;
}