Inspired by this post and by the underlying forest problem I had the genial ( grin ;) idea of just making the check horizontally and then repeat it on a rotated by 90° AoA.
Rotating the AoA lead me to PDL and here troubles and stumbling started.
Firstly because once something is a piddle I cannot anymore use it as a comfortable perl datastrucure: if my piddle is a 2d array and I take a slice of it, a row, then I cannot use it as an array: @{ $x->slice('0,') } gives a nasty not an array reference error.
But I insisted and now I have the following code:
# see for the actual problem to solve: https://adventofcode.com/2022/d +ay/8 use strict; use warnings; use PDL; use PDL::NiceSlice; my $debug = 1; my $x = rcols( *DATA, {COLSEP => qr//}, []); is_visible( $x ); # piddle rotated 90° counterclockwise is_visible( $x->transpose()->slice( ':', '-1:0' ) ); sub is_visible{ my $pdl = shift; print "Working on the piddle: $pdl(skipping first and last rows)\n +" if $debug; # # the idea is to consider only rows from second one to last but on +e foreach my $nrow ( 1 .. nelem($pdl->slice('0,'))-2 ){ # # now the idea is to consider only from second element to last + but one print "in ROW $nrow considering only: ", $pdl->slice("1:-2,($n +row)"),"\n" if $debug; # # ..and now checking these elements.. foreach my $to_check ( $pdl->slice("1:-2,($nrow)")->dog ){ print "\tchecking $to_check\n" if $debug; # # ...against all other members of the rows # like in (PSEUDOCODE) assuming @array being the current r +ow # foreach my $idx_to_check ( 1 .. $#array-1 ){ # my $res = map{ my_check( $array[$idx_to_check], $arr +ay[$_]) }grep{ $idx_to_check != $_ } 0..$#array; # } } } } __DATA__ 30373 25512 65332 33549 35390
In the meanwhile his Anonymousness posted a PDL solution to the problem, code I dont understand at all.
Can someone be so kind to guide me expanding my own solution? If such is possible I'd like it explained plainly, just to let me understanding PDL a bit more.
As side note I have found PDL QuickRef but I suspect some syntax has changed in PDL since then (would be nice to have an updated version of this ;) and this tutorial
Thanks for reading
L*
In reply to first stumbling steps in PDL by Discipulus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |