{ $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } #### #!/usr/bin/perl use strict; use warnings; use List::Util qw{ none any }; # parse the input (piped in, or from filename on command line) my @a = map { chomp; [ split // ] } <>; # determine the dimensions my $ni = @a; my $nj = @{ $a[0] }; # sanity check die "mismatched lengths" if any { @$_ != $nj } @a; # assume all visible to start with my $v = $ni * $nj; for my $i (0 .. $ni - 1) { for my $j (0 .. $nj - 1) { my $t = $a[$i][$j]; # target next if none { $_ >= $t } map $a[$i][$_], 0 .. $j - 1; # look left next if none { $_ >= $t } map $a[$i][$_], $j + 1 .. $nj - 1; # look right next if none { $_ >= $t } map $a[$_][$j], 0 .. $i - 1; # look up next if none { $_ >= $t } map $a[$_][$j], $i + 1 .. $ni - 1; # look down # if not visible in any direction, then invisible --$v; } } print "Visible $v of $ni x $nj\n";