7th day both parts

This commit is contained in:
kplaczek
2021-12-07 21:01:47 +01:00
parent 3e54c2e17e
commit 5f33713103
4 changed files with 38 additions and 1 deletions

13
07/part2.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$input = explode(',', $input[0]);
$minFuelCost = INF;
foreach (range(min($input), max($input)) as $newPosition) {
$fuelCost = array_sum(array_map(fn($currentPosition) => (abs($currentPosition - $newPosition) * (abs($currentPosition - $newPosition) + 1)) / 2, $input));
if ($fuelCost < $minFuelCost) {
$minFuelCost = $fuelCost;
}
}
echo $minFuelCost; //91257582