Advertisement
musifter

AoC 2024, day 4, part 1 (Perl)

Dec 4th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.93 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. use Math::Vector::Real;
  9.  
  10. my ($vy,$vx) = Math::Vector::Real->canonical_base(2);
  11. my @Dirs = ($vy, $vx, -$vy, -$vx, $vx+$vy, $vx-$vy, -$vx+$vy, -$vx-$vy);
  12.  
  13. # Read in grid, adding sentinel ~s to right and bottom
  14. my @Grid = map { chomp; [split(//), '~'] } <>;
  15. push( @Grid, [('~') x $Grid[0]->@*] );
  16.  
  17. sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
  18.  
  19. my @xmas  = split( //, 'XMAS' );
  20. my $part1 = 0;
  21.  
  22. for (my $y = 0; $y < $#Grid; $y++) {
  23.     SQUARE:
  24.     for (my $x = 0; $x < $#Grid; $x++) {
  25.         my $coord = V($y,$x);
  26.         next SQUARE if (&grid_at($coord) ne 'X');
  27.  
  28.         DIR:
  29.         for (my $d = 0; $d < @Dirs; $d++) {
  30.             for (my $i = 1; $i < 4; $i++) {
  31.                 next DIR  if ($xmas[$i] ne &grid_at($coord + $i * $Dirs[$d]));
  32.             }
  33.             $part1++;
  34.         }
  35.     }
  36. }
  37.  
  38. say "Part 1: $part1";
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement