He wants a radiator , not a fireplace . <| sep |> First things first . This is NPhard , so you pretty much just have to check all possible ways to order the boxes to find your answer . With constraint programming CP it ' s fairly easy to specify these types of problems . Then by adding some additional constraints to eliminate symmetrical cases turning a square box around doesn ' t change the solution , turning the entire pallet around also doesn ' t change the solution , you can end up with good solutions surprisingly quickly . The main advantage of CP is that you don ' t need to worry too much about the algorithms , the main disadvantage is that it ' s like a black box and it ' s not clear why a different formulation of the problem is quicker .