From b39c1d5f1ca60563cbf956c72f6b37e83cdd7d62 Mon Sep 17 00:00:00 2001 From: kplaczek Date: Sun, 12 Dec 2021 09:22:32 +0100 Subject: [PATCH] 11th day part 2 --- 11/part2.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 11/part2.php diff --git a/11/part2.php b/11/part2.php new file mode 100644 index 0000000..f5c42d7 --- /dev/null +++ b/11/part2.php @@ -0,0 +1,65 @@ + 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]++; + } + } + } + } 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; +}