Files
advent_of_code_2021/07/part1.php
2021-12-07 21:01:47 +01:00

14 lines
382 B
PHP

<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$input = explode(',', $input[0]);
$minFuelCost = INF;
foreach ($input as $newPosition) {
$fuelCost = array_sum(array_map(fn($currentPosition) => abs($currentPosition - $newPosition), $input));
if ($fuelCost < $minFuelCost) {
$minFuelCost = $fuelCost;
}
}
echo $minFuelCost; //328187