Finit le jour 8 et le début du 9

This commit is contained in:
2021-12-09 17:31:46 +01:00
parent f1899b0640
commit c290b5fee6
3 changed files with 145 additions and 0 deletions

31
day_9/part_1.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
$grid = array_map(static function ($row) {
return str_split($row);
}, explode("\n", file_get_contents('./input.txt')));
$low_points = [];
foreach ($grid as $i => $row) {
foreach ($row as $j => $cell) {
$count = 0;
if (!array_key_exists($i - 1, $grid) || $grid[$i - 1][$j] > $cell) {
$count++;
}
if (!array_key_exists($i + 1, $grid) || $grid[$i + 1][$j] > $cell) {
$count++;
}
if (!array_key_exists($j - 1, $row) ||$row[$j - 1] > $cell) {
$count++;
}
if (!array_key_exists($j + 1, $row) || $row[$j + 1] > $cell) {
$count++;
}
if ($count === 4) {
$low_points[] = (int) $cell;
}
}
}
echo array_sum($low_points) + count($low_points);