The following is a quick stab at the problem. It doesn't claim to create the best possible mapping. In fact, it only takes two stabs at the problem - once in the horizontal and once in the vertical. But, it does provide quite a good starting point. It also works very quickly.

The file format is:

# X-size, Y-size, Number of these x,y,n

The code follows:

use strict; use warnings; use IO::File; ###################################################################### +########## my $fn = shift; my $fh = IO::File->new($fn) || die "Cannot open '$fn' for reading: $!\n"; my @squares; while (<$fh>) { chomp; my ($x, $y, $num) = split ','; ($x,$y) = ($y,$x) if $x > $y; push @squares, [ $x, $y, $x * $y ] for 1 .. $num; } $fh->close; print "Working with:\n"; print "\t$_->[0] x $_->[1]\n" for @squares; try(@squares); @$_[0,1] = @$_[1,0] for @squares; print "Working with:\n"; print "\t$_->[0] x $_->[1]\n" for @squares; try(@squares); ###################################################################### +########## sub try { my @squares = @_; @squares = sort { $b->[2] <=> $a->[2] || $a->[0] <=> $b->[0] || $b->[1] <=> $a->[1] } @squares; my $cur_x = 0; my $cur_y = 0; my $max_y = $squares[0][1]; my $cur_p_x = 0; my $cur_p_y = 0; my $max_p_y = 0; my @placement; my @layout; foreach my $square (@squares) { my ($x, $y) = @$square; for my $i ($cur_x .. $cur_x + $x - 1) { $layout[$i] += $y; } $placement[$cur_p_x][$cur_p_y] = $square; $cur_y += $y; if ($cur_y >= $max_y) { $cur_x += $x; $cur_y = 0; $cur_p_x++; $cur_p_y = 0; } else { $cur_p_y++; $max_p_y = $cur_p_y if $max_p_y < $cur_p_y; } } for (@layout) { $max_y = $_ if $max_y < $_; } my $waste = 0; for (@layout) { $waste += $max_y - $_; } my $max_x = scalar @layout; print "Square is $max_x x $max_y or ", $max_x * $max_y, " big +with a waste of $waste\n"; print "Layout:\n"; for my $j (0 .. $max_p_y) { foreach my $square (map { $_->[$j] || [] } @placement) { unless (@$square) { print "\t"; next; } print "$square->[0] x $square->[1]\t"; } print "\n"; } }

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: help with nesting by dragonchild
in thread help with nesting by stu96art

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.