Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use Math::Vector::Real;
- my ($vy,$vx) = Math::Vector::Real->canonical_base(2);
- my @Diag = ($vx+$vy, -$vx-$vy, $vx-$vy, -$vx+$vy);
- # Read in grid, adding sentinel ~s to right and bottom
- my @Grid = map { chomp; y/MS/01/; [split(//), '~'] } <>;
- push( @Grid, [('~') x $Grid[0]->@*] );
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- my $part2 = 0;
- for (my $y = 0; $y < $#Grid; $y++) {
- SQUARE:
- for (my $x = 0; $x < $#Grid; $x++) {
- my $coord = V($y,$x);
- next SQUARE if (&grid_at($coord) ne 'A');
- my @corners = map {int} grep {/[01]/} map {&grid_at($coord + $_)} @Diag;
- next SQUARE if (@corners < 4);
- $part2 += ($corners[0] ^ $corners[1]) & ($corners[2] ^ $corners[3]);
- }
- }
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement