Here's my brute-forcer:
sub bin { my ($size, @set) = @_; my $best = []; my @bins; _bin($size, \@set, \@bins, 0, $best); print "BEST: <@{[ map qq{[@$_]}, @$best ]}>\n"; } sub _bin { my ($size, $set, $bins, $i, $best) = @_; if (@$set == 0) { @$best = map [@$_], @$bins if @$bins > @$best; return; } my $rem = $size - sum($bins->[$i]); for (sort { $set->[$b] <=> $set->[$a] } grep { $set->[$_] <= $rem } +0 .. $#$set) { my $n = splice @$set, $_, 1; push @{ $bins->[$i] }, $n; _bin($size, $set, $bins, (sum($bins->[$i]) == $size) ? $i+1 : $i, +$best); _bin($size, $set, $bins, $i+1, $best) if 0 == grep { $set->[$_] <= + ($rem-$n) } 0 .. $#$set; pop @{ $bins->[$i] }; pop @$bins if @{ $bins->[$i] } == 0; splice @$set, $_, 0, $n; } } sub sum { my $s = 0; $s += $_ for @{ $_[0] }; $s; }

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

In reply to Re^3: Challenge: Twist On Bin Packing by japhy
in thread Challenge: Twist On Bin Packing by Limbic~Region

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.