A more Perlish cleaner version might look like:
use strict;
use warnings;
my @row = map {chomp; [split '']} <DATA>;
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
which outputs 21 - the value suggested by the challenge. Note that is only deals correctly with rectangular forests containing 4 or more trees.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|