Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions easy-box-packer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def pack(container:, items:)
items.sort_by { |h| h[:dimensions].sort.reverse }.reverse.each do |item|
# If the item is just too big for the container lets give up on this
if item[:weight].to_f > container[:weight_limit].to_f
errors << "Item: #{item} is too heavy for container"
errors << { item: item, reason: 'too heavy for container' }
next
end

Expand Down Expand Up @@ -49,7 +49,7 @@ def pack(container:, items:)
# If it can't be placed in this space, then it's just
# too big for the container and we should abandon hope
unless placement
errors << "Item: #{item} cannot be placed in container"
errors << { item: item, reason: 'cannot be placed in container' }
next
end
# Otherwise lets put the item in a new packing
Expand Down Expand Up @@ -183,11 +183,14 @@ def place(item, space)
# select rotation with smallest margin
final = possible_rotations_and_margins.sort_by { |a| a[:margin].sort }.first
return unless final
return {
placement = {
dimensions: final[:rotation],
position: space[:position],
weight: item[:weight].to_f
}
# retain optional attributes, i.e. item identifiers, from item to placement
item.each { |key, value| placement[key] ||= value }
return placement
end

def break_up_space(space, placement)
Expand Down
2 changes: 1 addition & 1 deletion easy-box-packer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
]
)
expect(packings[:packings].length).to eql(1)
expect(packings[:errors]).to eql(["Item: {:dimensions=>[40, 10, 34]} cannot be placed in container"])
expect(packings[:errors]).to eql([{:item =>{:dimensions=>[40, 10, 34]},:reason=>'cannot be placed in container'}])
end

it 'case 5' do
Expand Down