--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+
+my ($x, $y, $dir) = (0, 0, 'E');
+
+while (<>) {
+ my ($cmd, $num) = /\A(.)(\d+)/;
+ print "cmd=$cmd, num=$num: ";
+ if ($cmd eq 'F') {
+ $cmd = $dir;
+ }
+ if ($cmd eq 'R') {
+ $num += 90;
+ $dir =~ y/NESW/ESWN/ while $num -= 90;
+ } elsif ($cmd eq 'L') {
+ $num += 90;
+ $dir =~ y/NESW/WNES/ while $num -= 90;
+ } elsif ($cmd eq 'N') {
+ $y += $num;
+ } elsif ($cmd eq 'S') {
+ $y -= $num;
+ } elsif ($cmd eq 'E') {
+ $x += $num;
+ } elsif ($cmd eq 'W') {
+ $x -= $num;
+ }
+ print " at ($x, $y, $dir)\n";
+}
+
+print abs($x) + abs($y), "\n";
+
--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+
+my ($wx, $wy, $x, $y) = (10, 1);
+
+while (<>) {
+ my ($cmd, $num) = /\A(.)(\d+)/;
+ print "cmd=$cmd, num=$num: ";
+ if ($cmd eq 'F') {
+ $x += $num*$wx;
+ $y += $num*$wy;
+ } elsif ($cmd eq 'R') {
+ while (($num -= 90) >= 0) {
+ my $tmp = $wx;
+ $wx = $wy;
+ $wy = -$tmp;
+ }
+ } elsif ($cmd eq 'L') {
+ while (($num -= 90) >= 0) {
+ my $tmp = $wy;
+ $wy = $wx;
+ $wx = -$tmp;
+ }
+ } elsif ($cmd eq 'N') {
+ $wy += $num;
+ } elsif ($cmd eq 'S') {
+ $wy -= $num;
+ } elsif ($cmd eq 'E') {
+ $wx += $num;
+ } elsif ($cmd eq 'W') {
+ $wx -= $num;
+ }
+ print " at ($x, $y, $wx, $wy)\n";
+}
+
+print abs($x) + abs($y), "\n";
+