Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Uninitialized warnings trouble

by Anonymous Monk
on Dec 15, 2022 at 09:43 UTC ( [id://11148888]=note: print w/replies, xml ) Need Help??


in reply to Uninitialized warnings trouble

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

Replies are listed 'Best First'.
Re^2: Uninitialized warnings trouble
by Anonymous Monk on Dec 15, 2022 at 12:39 UTC

    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 ) } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found