aboutsummaryrefslogtreecommitdiff
path: root/challenge-075/shawn-wagner/perl/ch-2.pl
blob: e95f1ec47559a6f26909c38974797e56265894c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env perl
use warnings;
use strict;
use utf8;
use open qw/:std encoding(UTF-8)/;
use feature qw/say/;
use List::Util qw/max/;

# Fancy unicode histogram printer
sub histogram {
  my @A = @_;
  my $rows = max @A;
  for my $row (reverse (1 .. $rows)) {
    print $row, "│";
    for my $col (@A) {
      print $col >= $row ? "█" : " ", " ";
    }
    print "\n";
  }
  print " └", "──" x @A, "\n  ";
  print $_, " " for @A;
  print "\n";
}

sub task2 {
  my @A = @_;
  histogram @A;
  my $maxsize = 0;
  for my $left (0 .. $#A) {
    for my $top (1 .. $A[$left]) {
      for my $right ($left+1 .. $#A) {
        last if ($A[$right] < $top);
        my $size = ($right - $left + 1) * $top;
        $maxsize = max $maxsize, $size;
      }
    }
  }
  say "Largest rectangle area: $maxsize";
}

task2 2, 1, 4, 5, 3, 7;
print "\n";
task2 3, 2, 3, 5, 7, 5;