Advertisement
musifter

AoC day 12 (pt1), Perl

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