5086

Two Dimensions, Fewest Pallets

Algorithm
Hard
Pro
bin-packing
knapsack
optimization
logistics

Two Dimensions, Fewest Pallets

The Christmas rush is on, and every truck that leaves the warehouse is money: each pallet slot costs freight, so dispatch wants shipment lines packed onto as few pallets as the physics allow. A pallet has two hard limits — a weight cap and a volume cap — and a load plan that breaches either one gets rejected at the dock. You are building the load planner: the little bin-packing engine that turns a pile of shipment lines into a tight, valid, reproducible pallet plan.

Requirements

The starter contains one table — keep it exactly as shipped, the grading tests seed and read it:

"Pallet Shipment Line" — one row per shipment line waiting to be palletized:

  • "Line No." — Integer, the primary key
  • Weight — Decimal, what the line weighs; never changes
  • Volume — Decimal, the space the line takes; never changes
  • "Pallet No." — Integer, your output: which pallet the line rides on; 0 until you assign it

Create a codeunit named "Pallet Loader" with one public procedure:

procedure BuildLoad(var PalletShipmentLine: Record "Pallet Shipment Line"; MaxWeight: Decimal; MaxVolume: Decimal): Integer

Rules:

  1. A load plan is valid when every line sits on exactly one pallet and, on every pallet, the lines' Weight values sum to at most MaxWeight and their Volume values sum to at most MaxVolume. Both caps bind on every pallet, always — a pallet with spare weight capacity still overflows when the volumes breach their cap.
  2. If any single line has Weight greater than MaxWeight or Volume greater than MaxVolume, no load plan exists: fail with an error whose text contains the word fit (lowercase) and the "Line No." of the offending line, and leave every "Pallet No." at 0 — a failed build writes nothing. If several lines are oversized, report the lowest "Line No." among them (the grading fixtures only ever seed one, so this tie-break is not graded).
The rest of the statement and 3 hints Unlock with Pro
ALBusiness Central 28.5
Pro
Pro problem

Solve it with Pro

You’re reading the opening of the statement. Pro unlocks the whole problem:

  • The full statement
  • Starter code in the browser editor
  • 11 grading tests, run on Business Central 28.5
  • 3 hints
  • The reference solution

Pro also opens all 92 Pro problems and lifts the daily limits on compile checks and submissions.

$10 / month
or $100 / year
Upgrade
This problem is part of Pro
Pro unlocks the full statement, tests, hints and solution. The code editor works on a computer.
Upgrade