Advertisement
musifter

AoC 2024, day 4, part 2 (Perl)

Dec 4th, 2024 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.86 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 @Diag = ($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; y/MS/01/; [split(//), '~'] } <>;
  15. push( @Grid, [('~') x $Grid[0]->@*] );
  16.  
  17. sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
  18.  
  19. my $part2 = 0;
  20.  
  21. for (my $y = 0; $y < $#Grid; $y++) {
  22.     SQUARE:
  23.     for (my $x = 0; $x < $#Grid; $x++) {
  24.         my $coord = V($y,$x);
  25.         next SQUARE if (&grid_at($coord) ne 'A');
  26.  
  27.         my @corners = map {int} grep {/[01]/} map {&grid_at($coord + $_)} @Diag;
  28.         next SQUARE if (@corners < 4);
  29.  
  30.         $part2 += ($corners[0] ^ $corners[1]) & ($corners[2] ^ $corners[3]);
  31.     }
  32. }
  33.  
  34. say "Part 2: $part2";
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement