adventofcode/2024/04/Part2.pm

43 lines
864 B
Perl
Raw Permalink Normal View History

2024-12-04 20:46:05 +00:00
package Part2;
use 5.36.0;
no warnings qw/ uninitialized /;
use Math::VectorXYZ::2D;
use List::Util qw/ sum pairmap /;
use List::MoreUtils qw/ part zip /;
sub find_word ( $grid, @locs ) {
for (@locs) {
return 0 if $_->[0] < 0 or $_->[1] < 0;
}
my $letters = join '', sort map { $grid->[ $_->[0] ][ $_->[1] ] } @locs;
return $letters eq 'MS';
}
sub solve ($grid) {
my $total = 0;
for my $x ( 0 .. $grid->$#* ) {
for my $y ( 0 .. $grid->[0]->$#* ) {
if ( $grid->[$x][$y] eq 'A' ) {
$total++ if 2 == grep {
find_word(
$grid,
Vec( $x, $y ) - Vec(@$_),
Vec( $x, $y ) + Vec(@$_)
)
} [ 1, 1 ], [ -1, 1 ];
}
}
}
return $total;
}
1;