Answer ⏱ 171ms
Part 1 :
9565386
Part 2 :
857986849428
function solve($galaxies, $empty_x, $empty_y, $expansion) {
foreach ($galaxies as [&$x, &$y]) {
$x += $empty_x->filter(fn ($ex) => $ex < $x)->count() * ($expansion - 1);
$y += $empty_y->filter(fn ($ey) => $ey < $y)->count() * ($expansion - 1);
}
$total = 0;
for ($i = 0; $i < count($galaxies) - 1; $i++)
for ($j = $i + 1; $j < count($galaxies); $j++)
$total += manhattan($galaxies[$i], $galaxies[$j]);
return $total;
}
$grid = grid($input);
$empty_y = $grid->rows()->filter(fn ($l) => !$l->hasValue("#"))->keys();
$empty_x = $grid->columns()->filter(fn ($l) => !$l->hasValue("#"))->keys();
$galaxies = $grid->searchAll("#");
$solution_1 = solve($galaxies, $empty_x, $empty_y, 2);
$solution_2 = solve($galaxies, $empty_x, $empty_y, 1000000);