That problem statement is a variation of the Knapsack problem. Your initial constraint is the 8.5x11 size and you have an additional constraint of aligning the sides as much as possible. The code I posted does accidentally align the sides of the rectangles, but provides no constraints on the size.

I might play around with it a bit to provide the paper-size constraint, but you'd be better off following the googlinks that others have provided.

Update: You can try the following algorithm to see if it helps:

  1. Get your paper size
  2. Get your list of rectangles. Collapse it to (x,y,n) if you don't already have it that way.
  3. Eliminate all rectangles that cannot fit on the paper. E.g., 1x12 won't fit on 8.5x11, but will fit on 8.5x14
  4. Reduce the number of rectangles of a given size to the most that can fit on your paper. For example, two 6x11 rectangles cannot fit on the same 8.5x11 piece of paper
  5. Expand the list of rectangles so that (x,y,n) => (x,y) x n
  6. Order the remaining rectangles by area, smallest first, then by X, smallest first
  7. Start tiling in the X direction, filling the Y direction and see what comes out
  8. Re-order the rectangles by area, then Y (smallest first in both values)
  9. Repeat, but in the Y direction filling the X direction.
  10. Repeat the two tilings, swapping each particular rectangle's orientation. (($x, $y) = ($y, $x);) This will result in N! tilings, where N is the total number of rectangles (of all sizes) you're working with
  • This problem is O(n!), which is pretty crappy. It's also (at least) NP-hard, so you can't really prove this algorithm will provide the best solution. It might provide a good base for a human to take and improve upon, but that's the best it could possibly do.

    ------
    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: Re: 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.