Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Uninitialized warnings trouble

by GrandFather (Saint)
on Dec 14, 2022 at 04:09 UTC ( [id://11148850]=note: print w/replies, xml ) Need Help??


in reply to Uninitialized warnings trouble

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148850]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found