Commence le jour 8

This commit is contained in:
Clement Desmidt 2022-12-09 16:41:18 +01:00
parent c563849e0c
commit c391988ffe
1 changed files with 43 additions and 0 deletions

43
day_8/part_1.php Normal file
View File

@ -0,0 +1,43 @@
<?php
$map = array_map('str_split', explode("\n", file_get_contents('./input.txt')));
$numRows = count($map);
$numCols = count($map[0]);
$visibles = ($numRows - 2) * 2 + ($numCols * 2); // edges
for ($i = 1; $i <= $numRows - 2; $i++) {
for ($j = 1; $j <= $numCols - 2; $j++) {
if (isVisible($i, $j, $map)) {
$visibles++;
echo sprintf('{%u, %u} {%u} is visible %s', $i, $j, $map[$i][$j], "\n");
} else {
echo sprintf('{%u, %u} {%u} is NOT visible %s', $i, $j, $map[$i][$j], "\n");
}
}
}
echo $visibles;
/**
* @param int $x
* @param int $y
* @param array $map
*
* @return bool
*/
function isVisible(int $x, int $y, array $map): bool
{
$currentNumber = (int)$map[$x][$y];
$row = $map[$x];
if (max(array_slice($row, 0, $y)) < $currentNumber || max(array_slice($row, $y + 1)) < $currentNumber) {
return true;
}
array_unshift($map, null);
$map = array_map(...$map);
$col = $map[$y];
return max(array_slice($col, 0, $x)) < $currentNumber || max(array_slice($col, $x + 1)) < $currentNumber;
}