Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- $| = 1;
- use List::AllUtils qw(pairwise sum);
- my %Dirs = ( 'E' => [1,0], 'N' => [0,1], 'W' => [-1,0], 'S' => [0,-1] );
- my @Widder = ([ [ 1, 0], [ 0, 1] ], # 0 tau
- [ [ 0,-1], [ 1, 0] ], # tau / 4
- [ [-1, 0], [ 0,-1] ], # tau / 2
- [ [ 0, 1], [-1, 0] ]); # 3 tau / 4
- my @Pos = (0,0);
- my @Grad = (10,1);
- while (<>) {
- m#(\w)(\d+)#;
- if ($1 eq 'F') {
- @Pos = pairwise { $a + $2 * $b } @Pos, @Grad;
- } elsif ($1 eq 'L' || $1 eq 'R') {
- my @rot = @{$Widder[ $2 / 90 * (($1 eq 'R') ? -1 : 1) ]};
- @Grad = map { sum pairwise { $a * $b } @Grad, @$_ } @rot;
- } else {
- @Grad = pairwise { $a + $2 * $b } @Grad, @{$Dirs{$1}};
- }
- }
- print "Part 2: ", (sum map { abs } @Pos), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement