$points = $input->numbers->chunk(2);
$areas = [];
$infinite = [];
$solution_2 = 0;
$tl = [$points->column(0)->min(), $points->column(1)->min()];
$br = [$points->column(0)->max(), $points->column(1)->max()];
// Loop through all points in the bounding box with an extra border of 1
for ($x = $tl[0] - 1; $x <= $br[0] + 1; $x++) {
for ($y = $tl[1] - 1; $y <= $br[1] + 1; $y++) {
$distances = $points->map(fn ($point) => abs($point[0] - $x) + abs($point[1] - $y));
$closest = $distances->keep($distances->min())->keys();
// If sum of all distances is within the limit, increment solution 2
if ($distances->sum() < 10_000) $solution_2++;
// More that one closest point -> not in any area
if ($closest->count() > 1) continue;
// If point is on the border, register infinite zone that we'll exclude later
if ($x == $tl[0] - 1 || $x == $br[0] + 1 || $y == $tl[1] - 1 || $y == $br[1] + 1) {
$infinite[] = $closest->first();
}
$areas[$closest->first()] = ($areas[$closest->first()] ?? 0) + 1;
}
}
$solution_1 = set($areas)->removeKeys($infinite)->max();