7th day both parts
This commit is contained in:
1 parent
3e54c2e17e
commit
5f33713103
4 files changed
+38
-1
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user