package Part1; 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 read_file (@lines) { return [ map { [ split '' ] } @lines ]; } my @directions; for my $x ( -1 .. 1 ) { for my $y ( -1 .. 1 ) { push @directions, Vec( $x, $y ) if $x or $y; } } sub find_word ( $grid, $letters, $loc, $dir ) { return 1 unless @$letters; $loc += $dir; return 0 if $loc->[0] < 0 or $loc->[1] < 0; return 0 if $grid->[ $loc->[0] ][ $loc->[1] ] ne shift @$letters; return find_word( $grid, $letters, $loc, $dir ); } sub solve ($grid) { my $total = 0; for my $x ( 0 .. $grid->$#* ) { for my $y ( 0 .. $grid->[0]->$#* ) { if ( $grid->[$x][$y] eq 'X' ) { $total += find_word( $grid, [qw/M A S/], Vec( $x, $y ), $_ ) for @directions; } } } return $total; } 1;