We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Maybe so (matrix of any size, but not less than the size of the hourglass):
#!/usr/bin/perlusewarnings;usestrict;useutf8;#hourglass dimensions, min 3x3my$hg_dim={'x'=>5,#width, must be odd'y'=>4,#height};$hg_dim->{wdht}=$hg_dim->{x}-1;#i$hg_dim->{hght}=$hg_dim->{y}-1;#j$hg_dim->{cntr}=sprintf("%u",$hg_dim->{x}/2);my$arr=[];while(<STDIN>){chomp;push$arr,[split(' ',$_)];}# ...or $#{$arr-$hg_dim-1>[0]}+1 - $hg_dim-1my$max_x=(scalar@{$arr->[0]})-($hg_dim->{wdht});my$max_y=(scalar@{$arr})-($hg_dim->{hght});# for neg nums, or add $sum on array and sort itmy$max_sum=-1000;for(my$y=0;$y<$max_y;$y++){for(my$x=0;$x<$max_x;$x++){my$sum=0;for(my$i=$x;$i<=$x+$hg_dim->{wdht};$i++){$sum+=$arr->[$y]->[$i];#upper row$sum+=$arr->[$y+$hg_dim->{hght}]->[$i];#lowest row}for(my$j=$y+1;$j<$y+$hg_dim->{hght};$j++){$sum+=$arr->[$j]->[$x+$hg_dim->{cntr}];#column}$max_sum=($sum>$max_sum)?$sum:$max_sum;}}print$max_sum,$/;
It's complete all Test Cases and I tested it on this matrix (7x8):
Day 11: 2D Arrays
You are viewing a single comment's thread. Return to all comments →
Maybe so (matrix of any size, but not less than the size of the hourglass):
It's complete all Test Cases and I tested it on this matrix (7x8):