advent_of_code_2021/day_9/part_1.php

31 lines
800 B
PHP

<?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);