implode('', $l), $input)); echo "\n\n"; } echo $flashNumberTotal; function checkForFlash($input) { $flashed = []; do { for ($i = 1; $i < count($input[0]) - 1; $i++) { for ($j = 1; $j < count($input) - 1; $j++) { if ($input[$i][$j] > 9 && !isset($flashed[$i . ',' . $j])) { $flashed[$i . ',' . $j] = [$i, $j]; $input[$i][$j] = 0; if (!isset($flashed[$i - 1][$j - 1])) $input[$i - 1][$j - 1]++; if (!isset($flashed[$i - 1][$j])) $input[$i - 1][$j]++; if (!isset($flashed[$i - 1][$j + 1])) $input[$i - 1][$j + 1]++; if (!isset($flashed[$i][$j - 1])) $input[$i][$j - 1]++; if (!isset($flashed[$i][$j])) $input[$i][$j]++; if (!isset($flashed[$i][$j + 1])) $input[$i][$j + 1]++; if (!isset($flashed[$i + 1][$j - 1])) $input[$i + 1][$j - 1]++; if (!isset($flashed[$i + 1][$j])) $input[$i + 1][$j]++; if (!isset($flashed[$i + 1][$j + 1])) $input[$i + 1][$j + 1]++; // printInput($input); // print_r($flashed); // readline(); } } } } while (canFlash($input)); foreach ($flashed as $flash) { $input[$flash[0]][$flash[1]] = 0; } return [$input, count($flashed)]; } function canFlash($input) { for ($i = 1; $i < count($input[0]) - 1; $i++) { for ($j = 1; $j < count($input) - 1; $j++) { if ($input[$i][$j] > 9) { return true; } } } return false; } function printInput($input) { for ($i = 1; $i < count($input[0]) - 1; $i++) { for ($j = 1; $j < count($input) - 1; $j++) { // echo str_pad($input[$i][$j], '3', ' ', STR_PAD_LEFT); echo $input[$i][$j]; } echo "\n"; } echo "\n"; } //echo implode("\n", array_map(fn($l) => implode('', $l), $input));