It appears that I have confused a few people here. What I need "minimized" is the aggregate height per column as summed from @height data.
Given the heights of qw/ 10 10 15 20 10 10 /, I could conceivably construct a table with no column having greater than 20 items in it:
10 15 20 10
10 10
The following solution, would fail because one of the columns has 25 items:
10 10 20 10
15 10
Part of the problem, I suspect, is that I had a bug in my original code because I accidentally posted the wrong version. The @distribution array needs to be reset every iteration. The following is correct:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my @height = qw/ 10 15 25 30 10 13 /;
my @columns = ($height[0],0,0,$height[-1]);
my $curr_height = 0;
# set this unreasonably high to ensure that subsequent
# heights will be lower
$curr_height += $_ foreach @height;
my @distribution;
for my $a (0..1) {
$columns[$a] += $height[1];
for my $b (0..2) {
$columns[$b] += $height[2];
for my $c (1..3) {
$columns[$c] += $height[3];
for my $d (2..3) {
$columns[$d] += $height[4];
my $this_height = ( sort @columns )[-1];
my $valid_dist;
foreach ( @columns ) {
$valid_dist = $_;
last if ! $valid_dist;
}
if ( $valid_dist and $this_height < $curr_height ) {
$curr_height = $this_height;
@distribution = ([1],[],[],[6]);
push @{$distribution[$a]}, 2;
push @{$distribution[$b]}, 3;
push @{$distribution[$c]}, 4;
push @{$distribution[$d]}, 5;
}
$columns[$d] -= $height[4];
}
$columns[$c] -= $height[3];
}
$columns[$b] -= $height[2];
}
$columns[$a] -= $height[1];
}
print Dumper \@distribution;
I hope that's clear, now :)
I also have to add that there's a heck of a lot more discussion on this than I thought there would be.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. |