14 lines
382 B
PHP
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
|