Advertisement
musifter

AoC day 12 (pt 2), Perl

Dec 12th, 2020 (edited)
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.84 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. $| = 1;
  7.  
  8. use List::AllUtils  qw(pairwise sum);
  9.  
  10. my %Dirs   = ( 'E' => [1,0], 'N' => [0,1], 'W' => [-1,0], 'S' => [0,-1] );
  11. my @Widder = ([ [ 1, 0], [ 0, 1] ],     # 0 tau
  12.               [ [ 0,-1], [ 1, 0] ],     #   tau / 4
  13.               [ [-1, 0], [ 0,-1] ],     #   tau / 2
  14.               [ [ 0, 1], [-1, 0] ]);    # 3 tau / 4
  15.  
  16. my @Pos  = (0,0);
  17. my @Grad = (10,1);
  18.  
  19. while (<>) {
  20.     m#(\w)(\d+)#;
  21.  
  22.     if ($1 eq 'F') {
  23.         @Pos = pairwise { $a + $2 * $b } @Pos, @Grad;
  24.     } elsif ($1 eq 'L' || $1 eq 'R') {
  25.         my @rot = @{$Widder[ $2 / 90 * (($1 eq 'R') ? -1 : 1) ]};
  26.         @Grad = map { sum pairwise { $a * $b } @Grad, @$_ } @rot;      
  27.     } else {
  28.         @Grad = pairwise { $a + $2 * $b } @Grad, @{$Dirs{$1}};
  29.     }
  30. }
  31.  
  32. print "Part 2: ", (sum map { abs } @Pos), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement