- floor: documentation ( source)
- array_keys: documentation ( source)
- min: documentation ( source)
- json_encode: documentation ( source)
<?php
test(2000);
test(1845);
test(2340);
test(3000);
// Examples:
// Door 2000 1845 3000
// 615mm panel 1 3 5
// 495mm panel 3 0 0
// panel loss 100 0 75
function calcOptimalPanels ($doorHeight) {
$bigHeight = 615;
$smallHeight = 495;
$bigFit = floor($doorHeight / $bigHeight);
$smallFit = floor($doorHeight / $smallHeight);
$options = [];
for ($big = 0; $big <= $bigFit; $big++) {
for ($small = 0; $small <= $smallFit; $small++) {
$waste = $bigHeight * $big + $smallHeight * $small - $doorHeight;
if ($waste === 0) // Get first combination without waste
return getFormattedResult($big, $small, $waste);
if ($waste < 0) // Omit combinations smaller then door
continue;
$options[$waste] = getFormattedResult($big, $small, $waste);
}
}
$minWaste = min(array_keys($options));
return $options[$minWaste];
}
function getFormattedResult($big, $small, $waste) {
return ['615mm' => $big, '495mm' => $small, 'waste' => $waste];
}
function test($doorHeight) {
echo $doorHeight . ': ' . json_encode(calcOptimalPanels($doorHeight)) . "\n";
}