Advent of code 2022/8
Ajax Direct

Answer

Part 1 :
Part 2 :
$grid       = grid($input);
$directions = set(["up", "right", "down", "left"]);

// ==================================================
// > PART 1
// ==================================================
$solution_1 = $grid->filter(fn ($tree, $xy) =>
    !$directions->filter(fn ($dir) => $grid->slice($xy, $dir)->max() < $tree)->empty()
)->cells()->count();

// ==================================================
// > PART 2
// ==================================================
$solution_2 = $grid->map(fn ($tree, $xy) =>
    $directions->map(fn ($dir) => (($stopped = $grid->slice($xy, $dir)->searchNumberOver($tree)) === false ? $grid->slice($xy, $dir)->count() : $stopped + 1))->multiply()
)->cells()->max();