Excellent, well done. :)

I see you are already writing quite different code from what you started with. Next it might be worth thinking about whether you can rearrange things to avoid having to repeat this block over and over:

{ $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; }

Note also that you don't need syswrite STDOUT here, print is quite enough - you just need to set $| = 1; at the start to disable buffering.

For what it's worth, here is my version which uses almost the same approach:

#!/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; # loo +k left next if none { $_ >= $t } map $a[$i][$_], $j + 1 .. $nj - 1; # loo +k right next if none { $_ >= $t } map $a[$_][$j], 0 .. $i - 1; # loo +k up next if none { $_ >= $t } map $a[$_][$j], $i + 1 .. $ni - 1; # loo +k down # if not visible in any direction, then invisible --$v; } } print "Visible $v of $ni x $nj\n";

If the speed of this algorithm ever becomes an issue, it also becomes interesting to consider a more dynamic approach: with a companion array of "visible/invisible" booleans, you can make just two passes over each row (once in each direction, or combined) and two over each column, then read the result out of the companion array. That reduces the algorithmic complexity from O(n^3) to O(n^2) for an n * n array.


In reply to Re^2: Uninitialized warnings trouble by hv
in thread Uninitialized warnings trouble by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.