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