Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Uninitialized warnings trouble

by Anonymous Monk
on Dec 13, 2022 at 16:42 UTC ( [id://11148831]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to solve day 8, part a. of Advent of Code: https://adventofcode.com/2022/day/8 . I am being flooded with uninitialized value warnings for lines 52, 55, 72, and 75. Here is one:

Use of uninitialized value in numeric lt (<) at ./day8a-l.pl line 55, <$line> line 98

My input is 99 lines with 99 numbers ranging from 0-9 on each line(no whitespace). Here is my code:

#!/usr/bin/perl use strict; use warnings; my @row; my $current = 0; my $visible = 198; my $found = 0; open(my $lines, '<', 'input8.txt'); foreach (<$lines>) { chomp; for my $l (0..length()-1) { my ($digit) = /^.{$l}(.)/; if (defined $digit) { $row[$current][$l] = $1; } } $current += 1; } close $lines; open(my $line, '<', 'input8.txt'); $current = 0; while (<$line>) { chomp; if ($current > 0 and $current < 98) { $visible += 2; for (my $i = 1; $i < 98; $i++) { my $counter = 1; $found = 0; while ($row[$current][$i-$counter] < $row[$current][$i] an +d $found == 0) { $counter += 1; if ($counter == $i) { $visible += 1; $found = 1; $counter = 1; } } while ($row[$current][$i+$counter] < $row[$current][$i] an +d $found == 0) { $counter += 1; if ($counter == $i) { $visible += 1; $found = 1; $counter = 1; } } while ($row[$current-$counter][$i] < $row[$current][$i] an +d $found == 0) { $counter += 1; if ($counter == $current) { $visible += 1; $found = 1; $counter = 1; } } while ($row[$current+$counter][$i] < $row[$current][$i] an +d $found == 0) { $counter += 1; if ($counter == $current) { $visible += 1; $found = 1; $counter = 1; } } } } $current += 1; } close $line; print $visible . "\n";

I'd be really grateful for any suggestions. Thanks,

L

Replies are listed 'Best First'.
Re: Uninitialized warnings trouble
by hv (Prior) on Dec 13, 2022 at 19:06 UTC

    I am being flooded with uninitialized value warnings for lines 52 ...

    Let's start with this one: your previous loop (which I visualize as "looking to the left") is working ok. In that case, you are testing visibility of $row[$current][$i], and looking at the tree $counter to the left of it as $counter ranges from 1 to $i. So you check from $row[$current][$i - 1] down to $row[$current][0].

    In this loop (which I visualize as "looking to the right") you want to check each tree $counter to the right, so you should end up ranging from row[$current][$i + 1] to $row[$current][99] $row[$current][98]. But you are letting $counter run from 1 to $i again, so you actually check from row[$current][$i + 1] to $row[$current][$i + $i].

    If you fix that error, it should then be obvious what you need to do to fix the remaining two loops, and I think it is possible you'll even get the correct answer.

    When you do, I would love for you to come back with your finished code - I'd like to give you tips on how to improve the code, but I don't want to spoil the fun and learning experience of solving the problem first. :)

    Update 2022-12-13 19:06: correct index from 99 to 98

      Thanks! I fixed my while loops and am not getting any warnings. I haven't got the answer yet, though.

      $counter = $i + 1; while ($forest[$current][$counter] < $forest[$current][$i] and $fo +und == 0) { $counter += 1; if ($counter == 99) { $visible += 1; $found = 1; $counter = 1; } }

        Oh, I had a typo/thinko in my previous response - you were correctly counting from 0 to 98, but I wrongly suggested you should go up to $row[$counter][99] when I should have said 98. (So I think you should still be getting a few warnings.)

        My output is: 1673

        That's not what I get, but it isn't very far out. Maybe you could show the rest of the changes you made?

        It would probably be more practical to start with a smaller grid, so that you can more easily see what the correct answer is - which would mean generalizing your code to discover the size when you read the input file, but that's a useful thing to do in any case. Then you could test your code against some simple cases, like:

        222 222 (8 visible) 222 222 122 (9 visible) 222 222 221 (9 visible) 222 212 222 (9 visible) 222 222 222 (9 visible) 212 222 212 (8 visible) 222 22222 21112 (12 visible, test horizontal bounds) 22222 222 212 212 (12 visible, test vertical bounds) 212 222 etc.
Re: Uninitialized warnings trouble
by GrandFather (Saint) on Dec 14, 2022 at 04:09 UTC

    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
Re: Uninitialized warnings trouble
by jwkrahn (Abbot) on Dec 13, 2022 at 21:17 UTC
    foreach (<$lines>) { chomp; for my $l (0..length()-1) { my ($digit) = /^.{$l}(.)/; if (defined $digit) { $row[$current][$l] = $1; } } $current += 1; }

    A more Perl way to write that would be:

    while ( <$lines> ) { chomp; push @row, [ split // ]; }

      Or

      @row = map [ split /\n?/ ], <$lines>;
Re: Uninitialized warnings trouble
by Anonymous Monk on Dec 14, 2022 at 04:47 UTC

    I changed my code pretty radically using " List::Util". I now get the right answer :)

    #!/usr/bin/perl use strict; use warnings; use List::Util qw(all); #use Time::HiRes qw(usleep); sub usleep {}; my @forest; my $current = 0; my $visible; my $found = 0; open(my $lines, '<', 'input8.txt'); while (<$lines>) { chomp; push @forest, [ split // ]; } close $lines; foreach my $current (0..@forest-1) { my $row = $forest[$current]; foreach my $i (0..@$row-1) { if ($current == 0 or $current == @forest-1 or $i == 0 or $i == + @$row-1) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } if (all { $_ < $row->[$i] } @$row[0..$i-1] or all { $_ < $row- +>[$i] } @$row[$i+1..@$row-1]) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } if (all { $_ < $row->[$i] } map { $forest[$_][$i] } (0..$curre +nt-1)) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } if (all { $_ < $row->[$i] } map { $forest[$_][$i] } ($current+ +1..@forest-1)) { $visible++; syswrite STDOUT, $forest[$current][$i] . 'V'; usleep 100000; next; } syswrite STDOUT, $forest[$current][$i] . ' '; usleep 100000; } syswrite STDOUT, "\n"; } print "There are $visible visible trees.\n";

      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.

      Fun little problem. Here's another entry in my "The only data structure needed is a multi-line string" collection.

      #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11148831 use warnings; sub quarterturn { my $new = ''; $new .= "\n" while s/.$/ $new .= $&; '' /gem; return $new; } my $forest = do { local (@ARGV, $/) = 'input8.txt'; <> }; my $visible = ''; for ( 0 .. 3 ) { $visible |= $forest =~ s{(.)(?=(.*))}{ $2 =~ /[$1-9]/ ^ 1 }ger; $_ = quarterturn for $forest, $visible; } my $totalvisible = $visible =~ tr/1//; # count ones print "$forest\n$visible\ntotal visible: $totalvisible\n";
        Nice code of yours!

        I will share an alternative to your regex, which is longer and uses (?{ ... }), but avoids repeated lookahead:
        for ( 0 .. 3 ) { my $max; $visible |= $forest =~ s { ^ (?{ $max = -1; }) (*FAIL) | (\d) } { length( $1 > $max and $max = $1 ) }germx; $_ = quarterturn for $forest, $visible; }
        $new .= "\n" while s/.$/ $new .= $&; '' /gem;

        Did you test this?

        $ time perl -e' use Data::Dumper; $Data::Dumper::Useqq = 1; my $x = "123456789\n"; print Dumper $x; my $new = ""; $new .= "\n" while $x =~ s/.$/ $new .= $&; '' /gem; print Dumper $new; ' $VAR1 = "123456789\n"; Out of memory! real 1m54.230s user 0m3.216s sys 0m9.332s

        BTW, my system has 32GB of RAM     ouch!

        Sorry, my mistake was leaving in the single quotes, oops.

Re: Uninitialized warnings trouble
by Anonymous Monk on Dec 15, 2022 at 09:43 UTC

    I think rectangular forest deserves a proper PDL solution. Answer is 1698, same as running GrandFather's code.

    use strict; use warnings; use PDL; my $trees = 1 + rcols *DATA, { COLSEP => qr//, KEEP => qr/^\d+$/ }, []; my $size = max shape $trees; $trees-> reshape(( $size ) x 2 ); my $SLTM = identity( $size ) -> cumusumover -> xor( identity $size ) -> transpose; my $t4 = $trees-> dummy( -1, 4 )-> copy; { my @s = map $t4-> slice( "X, X, $_" ), 0 .. 3; $s[ 1 ] .= $s[ 1 ]-> copy-> transpose; $s[ 2 ] .= $s[ 2 ]-> copy-> slice( '-1:0, X' ); $s[ 3 ] .= $s[ 3 ]-> copy-> transpose-> slice( '-1:0, X' ); } my $cummax = maxover $SLTM * $t4-> dummy( 1 ); my $seen = $t4 > $cummax; { my @s = map $seen-> slice( "X, X, $_" ), 0 .. 3; $s[ 1 ] .= $s[ 1 ]-> copy-> transpose; $s[ 2 ] .= $s[ 2 ]-> copy-> slice( '-1:0, X' ); $s[ 3 ] .= $s[ 3 ]-> copy-> slice( '-1:0, X' )-> transpose; } print $seen-> mv( -1, 0 )-> orover-> sum; __END__ 0021000302223413032321152215243115022004032344522041252513450244235444 +43305142410110142101223120110 ... skipped, see 11148843

      Don't know if next version with planes (tables) rotation subroutine is "cooler". It's longer, but maybe intention is more clear. It does proper rotation this time, instead of reflection, slightly more work and doesn't affect final result.

      use strict; use warnings; use PDL; sub turn_tables { my ( $pdl, $dir ) = @_; # dir = 0 => cw # 1 => ccw my @s = ( undef, map $pdl-> slice( "X, X, $_" ), 1 .. 3 ); my @a = $dir ? ( 1, 2, 3 ) : ( 3, 2, 1 ); $s[ $a[0]] .= $s[ $a[0]]-> copy -> transpose-> slice( '-1:0, X' ); # 90 $s[ $a[1]] .= $s[ $a[1]]-> copy -> slice( '-1:0, -1:0' ); # 180 $s[ $a[2]] .= $s[ $a[2]]-> copy -> slice( '-1:0, X' )-> transpose; # 270 } my $trees = 1 + rcols *DATA, { COLSEP => qr//, KEEP => qr/^\d+$/ }, []; my $size = max shape $trees; $trees-> reshape(( $size ) x 2 ); my $SLTM = identity( $size ) -> cumusumover -> xor( identity $size ) -> transpose; my $t4 = $trees-> dummy( -1, 4 )-> copy; turn_tables( $t4 ); my $cummax = maxover $SLTM * $t4-> dummy( 1 ); my $seen = $t4 > $cummax; turn_tables( $seen, 1 ); print $seen-> mv( -1, 0 )-> orover-> sum; __END__ 0021000302223413032321152215243115022004032344522041252513450244235444 +43305142410110142101223120110 ... skipped, see 11148843

        This next version of turn_tables seems less ugly.

        sub turn_tables { my ( $pdl, $dir ) = @_; # dir = 0 => forward # 1 => backward my @t = ( # transform sub { $_[0]-> copy-> transpose-> slice( '-1:0, X' )}, # 90 sub { $_[0]-> copy-> slice( '-1:0, -1:0' )}, # 180 sub { $_[0]-> copy-> transpose-> slice( 'X, -1:0' )}, # 270 ); @t = reverse @t if $dir; for ( 1 .. 3 ) { my $s = $pdl-> slice( "X, X, $_" ); $s .= $t[ $_ - 1 ]-> ( $s ) } }
Re: Uninitialized warnings trouble
by tybalt89 (Monsignor) on Dec 13, 2022 at 21:53 UTC

    Could you please post your input file and its score ?

      I'm not exactly sure how to do this:

      0021000302223413032321152215243115022004032344522041252513450244235444 +43305142410110142101223120110 1020303223411433302442330130520254534220054153046554262064522513102140 +55314024034341233110302320003 0030122034040114301201345543145524416350016410502114062046211141020215 +43144514102510421243314102033 3303134230020341400350012104411565545456250422626513565565442025004461 +21455112434243313013430141203 3312031211312300020551134302446100626540345201063634621552012103556011 +53542301323442223310134324300 2322012310210032543554114023103262066250115641141252022635044514512126 +00155351424154310240411213431 0330333333010115235440232061361455053042243256060203600062064555645135 +53512054354124523232201230441 1322010322343340442513342203453604024565416622535563757546623232652016 +41514121105025300453041111234 3143223413221044022350202065650442253635276177675745244754217635043410 +61551154250344125514014234344 1013230430234305433424405323243264136556575272156611166234556136643210 +05134300223542510253102211310 3204042425141221115250434445306254176567731172475127124443157674224121 +30351505513231104322032421303 2212041232051331420440562456542052317343715466167532173215564722236540 +44043612554152141554333102034 3403101552254521522166356343527125413663123262722621435223471111666467 +36001360102010220214000133002 0124040035150024164225001632442713662527115316153334373524614547773111 +42132000140205630042305222013 3324345523022034455035434245233274643625266572516876347455365217465231 +54717531643250132300541053000 0201303124253163626111304622171254254355452654677252563327772331371415 +24314631100051153314314343032 1121151245120023460113424331461245423568454838627563227855452836527237 +47276776403131622540014001524 3323540131010030310530222454312746276546773436386726434425634487827374 +64337546541546630431125021330 2203153201420641614065343652623772683737435277845752463255646762388167 +33767331345114363612344051535 1020411535260006014023256257665345435278824766533667275734487277756771 +46365732431425225562043130034 3500541143105330024266541251665752857578274222578723787663832675434848 +65317252532765440216543555512 2224404043530161447473474573685626685862858475535787324358875844634468 +62116134751240360645225435344 5542345461632626303134317622488766686633524368883745878562633438425266 +35237314231451062036605442520 5255313246626603562566143444383735757326999883667759899764473843383368 +27686445577222030363631240512 0555412610425334241773757567384434828469869797889789975593776792554638 +58237757327464422026230510130 5141232212402504424577447283473866376849535745464553447545483337932338 +65436751231314653541223102202 3554542506611146451445517857247532373783453754934539675597966758398374 +44334776625552412552302431302 2024452343332067677446538747242765869736688789496534739476865675654873 +25864733735677672241416513452 4454152232110616337674338652524835747446859745933349636689837456433548 +85658423263536531213102544202 2014054642511646541773857832246664739756746437679466484646789589693363 +35638578275154116223152222102 4014231403564416547455722668489655935539956676446489647478845444394935 +45472454437715336645303346422 1544403232407414277328674675649573363949858597666444956589859635834963 +99736552632162756445212536404 4416636630626613721228673645846553889774748575857786868895787996449497 +33463423826715534215154065204 4220361220162436511746635886938985833749689747785795859774785996433895 +37573534354622377452555411315 4304124033622725133487636735665789456459974547586944867588449585697867 +58359475575577532661126532300 5036530204412674227653534698679983397644495967769689964497774646448469 +38866332635583727771665263026 0062526315413316357687547533573847549886595858579565676666459755996534 +66655333342485345762600032403 2152055427757137328688423898855856468848584498585797779759975849974636 +98339932263363162431251615251 3422244507311151757626265465389554477964688857767788796976956999454463 +88444478355347827635732006646 0450111102262116336236585483655444984664687668786995976558975864885554 +35934935526883851537736604133 4403522122434313763828696344345684476545668958977968756797596675988988 +49334873844583742271767024403 5666323671214147622724487847557467999857599665596579896979776779554557 +54549393784586875275341562621 0041311671357574268824644867736997867899599577959855955595877755544799 +45637849747667752242212324244 0224110314327242625335547946645974464798695687675988996699695889848644 +57535986725263473577457413134 0364150053311625273465774875937984586689768887768887668566995988477765 +67686579476836656723623111342 5040013023315452676844395547768949579997885778766889698655589555479656 +87556849786463457244355044511 4642660742641265752768338647845466978857888789679669886786966866855767 +89843676844576623242451411411 6601305736423263663626547855496765745878768887696896967987999886786485 +54635745724364273772474666502 4135115213134376524226649486456495669799666766797786699687777767869584 +64356797545247763267365643645 4550331744732745644842853879958795955556865699979776988687698556865648 +47593489668858847744131410225 5603166755323446354433496639944998698665985688897988689679766796555974 +94679336978524332246567265361 1210531633422343524538377545699684655579686977667668889699666677964794 +57384444468264837376474515455 6040623135276426347682558468996485455596658866689668879999596597699784 +95794795825775373713613231451 0410311126227543328424397767369669946778886998986799889677796967588495 +89365579766676242256227420364 6301613631264767784554757996585459555985765688777686986685799887995968 +66465635488284555252726211044 4555636414271272833647587964759758946586776887897777698766679787479556 +77573575525588444241765111511 2564200635326452255428638849588444965967599997697696876788887888754498 +88945958948378756476127155054 5232561652175473626363255476865497676989579679859689878977776858665767 +44344388256886854362647342110 2052246067645567635442348746766594744689685768796685588566758556687798 +47849738247387744421321145610 0226513556156535524684866764596947447475758776568867766778585849889446 +78787576556747724125467333305 5263134243247171264867466446433975896644688655998588869966697885664894 +73798387676766873216657521426 4265304045224117625725676973759597479756696679586856798786595877649776 +58398697353283563215312244014 0235102457411317754368645796846998749799765677765577658667754695877574 +98544476554753722641656303654 2113414334422254145653328878993636786994955678889986756576596887444584 +93387343842346747477252626250 4065431521543356147582777485958368967866549487599999785695885557648386 +86653647362854723163226456101 3336406336173155354565253648494585587446568998555548978975688444465338 +66457545654847562377442214226 0400030532465343544387487239333659348784859974589555747859669966533539 +69666867276761661111124104550 4056025162062643651573786274955469356585945878695799589696867669868366 +87348285865231616336232000100 3041310302542336415782724758689534465455777666449569896799644896885543 +98956223433417637732661500540 1311646622253254417333663243454779546394759976586847664654879746995796 +65344326248136151472051130445 4523121542061746541386842773768894499457469546896468757775964869493659 +68888456226732332263263420000 0030101545512462232742824543453556733499676899868857954499893596334967 +28553885221437611730454554455 1314351264600176355447857543448743796953793663845739343577953986339674 +75353836864461424146264354142 3501545440450133525331735662577577873854486974849974545787359374996656 +28425542376452563635450224022 3024202243651636517652644468752873595336374984456394439695864664333843 +46337722662774436566624454214 4152424563211051736557337448863438265489446663386838763839734979987444 +88687446541256724244661531431 1553032061343141243725536186238572373465678575644647975933936755635364 +67777317174625640132203003240 3355215446525236246415754517856353523335879576475985356339488678386526 +36686573137515062356442045153 2353145002446366153423253462772566856538382267554938586834656837362833 +67654226655456541546644313125 2525205115310613056143646712128564736256424777328452787665736424252865 +72562373137446456005011330433 2415254551201160465437376423255436776232438842425344233278274463833764 +41727657314343125403632434002 3010452235512203561671176217553257722528452225462465884647848746857746 +76163655131632622146412415515 0111501333113366142514354164536323834543275343672882423287465376735437 +14263674263262001132354544412 3141040330003062565316662564335535358335383374638472774534765632276727 +15617272601302551120422305504 1042114420131416644023636456171634352455562282353378646566736263541514 +16721375243323635625100210424 4134223135025353626150541715114317753355747485265747287474537244721576 +32635263350024552502052340024 1312253010233044564241504045257654165655722723218353771672353173125516 +45334432406536160545434122120 3421310522324133253014510415167165417373462524136671156126422273125724 +33104525003114240240141232002 2444211013540054302324344203443232734213742256355513634632455612717434 +11452116466311034334101231020 2324044023043114151641514015534637311471111477521417326251525536153610 +46133341313265525232544143442 2013210335201334143123013263302530412672516144773225666454726564145450 +35321106361434351501123314104 2112320344425242423134533250463313531743523354227523647757465375133335 +34355120322320404054014303124 2240322332404305101355416520650524151541475656562316516533275114434241 +13100543020223335130010141400 0041101011335434405115232654240133414330436550225121726221403224341161 +04552043413352211021312421433 0003044332324024101512053335266130434635560214134021416341544045301123 +03306244402221312522221004042 2002044410201352320330325415162325504452211415206650163365360520631153 +35040344025054213324400312303 3002110224403101230555100532515052264522655563651001010140446452365052 +13550201425252404100302413231 3103124132100324220532420242433430601353404223133124035056311044301202 +33112324204414021331012401232 1320203413303040220535035223133420262111336100000205542136622441425512 +53124302245242110111033101203

      My output is:

      1673

        Using the code you posted and the data above I get the answer:

        2429

        This code also gives the same answer:

        #!/usr/bin/perl use strict; use warnings; # 2429 my @row = do { open my $lines, '<', 'input8.txt'; map [ /\d/g ], <$lines>; }; my $visible = 198; for my $current ( 0 .. $#row ) { if ( $current > 0 && $current < 98 ) { $visible += 2; COUNT: for my $i ( 1 .. 97 ) { my $counter = 1; while ( $row[ $current ][ $i - $counter ] < $row[ $current + ][ $i ] ) { if ( ++$counter == $i ) { ++$visible; next COUNT; } } while ( $row[ $current ][ $i + $counter ] < $row[ $current + ][ $i ] ) { if ( ++$counter == $i ) { ++$visible; next COUNT; } } while ( $row[ $current - $counter ][ $i ] < $row[ $current + ][ $i ] ) { if ( ++$counter == $current ) { ++$visible; next COUNT; } } while ( $row[ $current + $counter ][ $i ] < $row[ $current + ][ $i ] ) { if ( ++$counter == $current ) { ++$visible; next COUNT; } } } } } print "$visible\n";

        You said your answer is wrong. Is 1673 correct, or if wrong, what is the correct answer?

Re: Uninitialized warnings trouble
by Anonymous Monk on Dec 13, 2022 at 16:44 UTC

    Forgot to mention that my output is the wrong answer, too.

      A typical silicon valley program ("fake it until you make it") would solve it like that:

      print "Calculating...\n"; my $endtime = time + 25; my $x; while(time < $endtime) { $x++; # Todo: Implement better algo for wasting CPU cycles } open(my $ifh, '<', 'solution.txt') or die($!); while((my $line = <$ifh>)) { print $line; } close $ifh;

      Sorry, couldn't resist ;-)

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11148831]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found