function v($x, $y) {
global $input;
return (new Intcode($input, [$x, $y]))->run();
}
// ==================================================
// > PART 1 : Easy but slow (~4s)
// ==================================================
// $grid = new Grid;
// for ($x = 0; $x < 50; $x++) for ($y = 0; $y < 50; $y++)
// if (v($x, $y)) $grid->set([$x, $y], "#");
// ==================================================
// > PART 1 : More optimized : avoid a lot of useless checks
// ==================================================
// Find the starting x and y in the bottom right corner
$x = 49; $y = 49;
while (!v($x, $y)) $y--;
// Move toward 0, 0 by checking each column, decreasing y when necessary
$solution_1 = 0;
for (; $x >= 0 && $y >= 0; $x--) {
$r = $y;
$entered = false;
do {
if ($v = v($x, $r)) {
$entered = true;
$solution_1++;
}
$r--;
} while ($r >= 0 && (!$entered || $v));
if (!v($x - 1, $y)) $y--;
}
// ==================================================
// > PART 2 : Move the box by checking corners
// ==================================================
$x = 1000;
$y = 1000;
do {
$tr = v($x + 99, $y);
$bl = v($x, $y + 99);
if (!$bl) $x++; elseif (!$tr) $y++;
} while ($tr != 1 || $bl != 1);
$solution_2 = $x * 10000 + $y;