use strict; use warnings; my @row = map {chomp; [split '']} ; my $visible; die "Data too awful to contemplate\n" if @row < 2 || @{$row[0]} < 2; for my $col (1 .. $#{$row[0]} - 1) { my $tHigh = $row[0][$col]; my $bHigh = $row[-1][$col]; $visible += 2; # Edge trees always visible for my $depth (1 .. $#row - 1) { test(\$tHigh, \$row[$depth][$col]) if $tHigh != 9; test(\$bHigh, \$row[-$depth - 1][$col]) if $bHigh != 9; last if $tHigh == 9 && $bHigh == 9; } } for my $rowIdx (1 .. $#row - 1) { # Count down from top edge my $lHigh = $row[$rowIdx][0]; my $rHigh = $row[$rowIdx][-1]; $visible += 2; # Edge trees always visible for my $depth (1 .. $#row - 1) { test(\$lHigh, \$row[$rowIdx][$depth]) if $lHigh != 9; test(\$rHigh, \$row[$rowIdx][-$depth - 1]) if $rHigh != 9; last if $lHigh == 9 && $rHigh == 9; } } $visible += 4; # We didn't consider the corners so do that now. print $visible; sub test { my ($highest, $cell) = @_; return if $$highest >= $$cell; if ($$cell !~ /\./) { ++$visible; $$cell .= '.'; } $$highest = $$cell; return $$highest == 9; } __DATA__ 30373 25512 65332 33549 35390