From 1e9b596be10cf29dae6e767fcc2a31ea696533a1 Mon Sep 17 00:00:00 2001 From: kplaczek Date: Sun, 12 Dec 2021 00:11:23 +0100 Subject: [PATCH] 11th day part 1 --- 11/part1.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 11/part1.php diff --git a/11/part1.php b/11/part1.php new file mode 100644 index 0000000..9472474 --- /dev/null +++ b/11/part1.php @@ -0,0 +1,85 @@ + 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)); \ No newline at end of file