From: Adam Shelly Date: 2006-01-20T11:06:39+09:00 Subject: Re: Ruby Quiz #62 On 1/19/06, James Edward Gray II wrote: > This version of the code seems to fix at least this case: There's a small bug in your code, James: If you pass a box that fits the trunk exactly, you get an exception. The culprit is this line from #try_adding box > # score the remaining open space > score = rows[1..-2].map { |row| row[/_(_+)/, 1] }.compact. > map { |open| open.size }.inject { |sum, n| > sum + n } score is nil if there is no open space. you need to make it ... inject(0) {|sum,n| ... You've got a good heuristic, but it still makes mistakes. Here's a case that needs an extra trunk with your solution: C:\ruby\RUBYRE~1\Main\Quiz>more 662.txt 6x6 2x3 2x2 3x6 1x6 4x1 4x4 C:\ruby\RUBYRE~1\Main\Quiz>pack.rb 662.txt ###### ###### ###### ______ ###### ______ ####__ ####__ ####__ ####__ ______ ####__ ###_## ###_## ______ ______ ______ ______ Those 2 in the last trunk ought to be where in the 1st trunk, and the 1x6 should be in the 2nd trunk. I think it's impossible to have a perfect solution without an incredibly deep search. On a totally unrelated note - I discovered something odd when working on this solution: irb(main):010:0> r=1..9 => 1..9 irb(main):011:0> r.last => 9 irb(main):012:0> r.include? r.last => true irb(main):013:0> r=1...9 => 1...9 irb(main):014:0> r.last => 9 irb(main):015:0> r.include? r.last => false I really expected (1...9).last to return 8. I guess this is just another way ranges are screwy. -Adam