DEV Community

Discussion on: Public Solving: Elf Post Service package calculator

Collapse
 
lexlohr profile image
Alex Lohr

Any box can take 3 positions if you don't account for mirrored ones: laying on the top/bottom, the front/back or either side. Ruling out each of these positions directly should be faster than either sorting the proportions (the first solution I shortly considered) or perform lots of multiplication and finding the maximum size for each box.

type Box = { width: number, height: number, length: number };
const fitsOriented = (outer: Box, inner: Box) =>
  outer.width > inner.width &&
  outer.height > inner.height &&
  outer.length > inner.length;
// turn over the X axis: swap height and length
const turnX = (box: Box) =>
  ({ width: box.width, height: box.length, length: box.height });
// turn over the Z axis: swap width and length
const turnZ = (box: Box) =>
  ({ width: box.length, height: box.height, length: box.width });
const fits = (outer: Box, inner: Box) =>
  fitsOriented(outer, inner) || 
  fitsOriented(outer, turnX(inner)) ||
  fitsOriented(outer, turnZ(inner));
const solution = boxes.find((box) => fits(box, item));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Yeah technically indeed, however you might occur objects who fit diagonal in either of the three ways introducing another level.

Not really an issue for this puzzle, but in terms of finding the "best" one I got stuck on that as well.