From fa7653af0fe875d161c9fb8dd0a516ded61490ad Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Mon, 13 Dec 2021 10:41:36 +0100 Subject: [PATCH] :sparkles: Finit le jour 11 --- day_11/part_1.php | 54 +++++++++++++++++++++++++++++++++++++ day_11/part_2.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 day_11/part_1.php create mode 100644 day_11/part_2.php diff --git a/day_11/part_1.php b/day_11/part_1.php new file mode 100644 index 0000000..9f617ac --- /dev/null +++ b/day_11/part_1.php @@ -0,0 +1,54 @@ + $row) { + foreach ($row as $j => $cell) { + $grid[$k][$j]++; + } + } + + while(has_more_than_nine($grid)) { + foreach ($grid as $k => $row) { + foreach ($row as $j => $cell) { + if ($grid[$k][$j] > 9) { + foreach (range(-1, 1) as $r) { + foreach (range(-1, 1) as $c) { + if ($r === 0 && $c === 0) { + $grid[$k+$r][$j+$c] = 0; + } + if ( + array_key_exists($k+$r, $grid) && + array_key_exists($j+$c, $grid[$k+$r]) && + $grid[$k+$r][$j+$c] !== 0 + ) { + $grid[$k+$r][$j+$c]++; + } + } + } + $flashes++; + } + } + } + } + + echo sprintf('After step %u%s%s%s', $i + 1, "\n", implode("\n", array_map('implode', $grid)), "\n", "\n"); +} + +echo $flashes; + +function has_more_than_nine($grid) +{ + foreach ($grid as $row) { + foreach ($row as $cell) { + if ($cell > 9) { + return true; + } + } + } + + return false; +} \ No newline at end of file diff --git a/day_11/part_2.php b/day_11/part_2.php new file mode 100644 index 0000000..3a9dd43 --- /dev/null +++ b/day_11/part_2.php @@ -0,0 +1,68 @@ + $row) { + foreach ($row as $j => $cell) { + $grid[$k][$j]++; + } + } + + while(has_more_than_nine($grid)) { + foreach ($grid as $k => $row) { + foreach ($row as $j => $cell) { + if ($grid[$k][$j] > 9) { + foreach (range(-1, 1) as $r) { + foreach (range(-1, 1) as $c) { + if ($r === 0 && $c === 0) { + $grid[$k+$r][$j+$c] = 0; + } + if ( + array_key_exists($k+$r, $grid) && + array_key_exists($j+$c, $grid[$k+$r]) && + $grid[$k+$r][$j+$c] !== 0 + ) { + $grid[$k+$r][$j+$c]++; + } + } + } + } + } + } + } + + $steps++; + + echo sprintf('After step %u%s%s%s%s', $steps, "\n", implode("\n", array_map('implode', $grid)), "\n", "\n"); +} + +echo $steps; + +function has_more_than_nine($grid) +{ + foreach ($grid as $row) { + foreach ($row as $cell) { + if ($cell > 9) { + return true; + } + } + } + + return false; +} + +function are_synchronized($grid) +{ + foreach ($grid as $row) { + foreach ($row as $cell) { + if ($cell !== 0) { + return false; + } + } + } + + return true; +} \ No newline at end of file