✨ Finit le jour 11
This commit is contained in:
parent
23a90beb38
commit
fa7653af0f
54
day_11/part_1.php
Normal file
54
day_11/part_1.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
$grid = array_map('str_split', explode("\n", file_get_contents('./input.txt')));
|
||||
|
||||
$flashes = 0;
|
||||
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
foreach ($grid as $k => $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;
|
||||
}
|
68
day_11/part_2.php
Normal file
68
day_11/part_2.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
$grid = array_map('str_split', explode("\n", file_get_contents('./input.txt')));
|
||||
|
||||
$steps = 0;
|
||||
|
||||
while (!are_synchronized($grid)) {
|
||||
foreach ($grid as $k => $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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user