✨ Finit le jour 7
This commit is contained in:
parent
eee724e103
commit
f1899b0640
22
day_7/part_1.php
Normal file
22
day_7/part_1.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
$positions = array_map('intval', explode(',', file_get_contents('./input.txt')));
|
||||
|
||||
$min = min($positions);
|
||||
$max = max($positions);
|
||||
|
||||
$lowest_fuel = $max * count($positions);
|
||||
$best_position = 0;
|
||||
|
||||
for ($i = $min; $i <= $max; $i++) {
|
||||
$fuel_used = 0;
|
||||
foreach ($positions as $position) {
|
||||
$fuel_used += abs($position - $i);
|
||||
}
|
||||
if ($lowest_fuel > $fuel_used) {
|
||||
$lowest_fuel = $fuel_used;
|
||||
$best_position = $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo sprintf('Best position is %u with %u fuel used.', $best_position, $lowest_fuel);
|
22
day_7/part_2.php
Normal file
22
day_7/part_2.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
$positions = array_map('intval', explode(',', file_get_contents('./input.txt')));
|
||||
|
||||
$min = min($positions);
|
||||
$max = max($positions);
|
||||
|
||||
$lowest_fuel = $max * array_sum(range(1, $max));
|
||||
$best_position = 0;
|
||||
|
||||
for ($i = $min; $i <= $max; $i++) {
|
||||
$fuel_used = 0;
|
||||
foreach ($positions as $position) {
|
||||
$fuel_used += array_sum(range(1, abs($position - $i)));
|
||||
}
|
||||
if ($lowest_fuel > $fuel_used) {
|
||||
$lowest_fuel = $fuel_used;
|
||||
$best_position = $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo sprintf('Best position is %u with %u fuel used.', $best_position, $lowest_fuel);
|
Loading…
Reference in New Issue
Block a user