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