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 @Dirs = ($vy, $vx, -$vy, -$vx, $vx+$vy, $vx-$vy, -$vx+$vy, -$vx-$vy);
- # Read in grid, adding sentinel ~s to right and bottom
- my @Grid = map { chomp; [split(//), '~'] } <>;
- push( @Grid, [('~') x $Grid[0]->@*] );
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- my @xmas = split( //, 'XMAS' );
- my $part1 = 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 'X');
- DIR:
- for (my $d = 0; $d < @Dirs; $d++) {
- for (my $i = 1; $i < 4; $i++) {
- next DIR if ($xmas[$i] ne &grid_at($coord + $i * $Dirs[$d]));
- }
- $part1++;
- }
- }
- }
- say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement