Be My Navigator

Python 3, 137 bytes

v=n=0
d=1
for i in input()+'U':x="NUERSDW0123456789".find(i);c=x<7;v+=d*n*c;n=[n*10+x-7,0][c];d=[d,d**(x%2)*1j**(~-x/2)][c]
print(abs(v))

Try it online!

-9 bytes thanks to Jonathan Allan


JavaScript (ES6), 148 142 140 138 137 134 bytes

s=>s.replace(/\d+|./g,_=>~(i='NESW'.search(_))?d=i:~(i='URDL'.search(_))?d+=i:a[d&3]+=+_,a=[0,0,0,0])&&Math.hypot(a[1]-a[3],a[0]-a[2])

f=

s=>s.replace(/\d+|./g,_=>~(i='NESW'.search(_))?d=i:~(i='URDL'.search(_))?d+=i:a[d&3]+=+_,a=[0,0,0,0])&&Math.hypot(a[1]-a[3],a[0]-a[2])

for(test of [
  'N10',
  'N10E10',
  'N10S10',
  'NSEWUDLR10',
  'N100RR20E300D40L12',
  'ERR10LL20UN30D100',
]) console.log(f(test))

-2 bytes: Use .search() instead of .indexOf() (@Shaggy)

-1 byte: Rearrange program to remove enclosing parentheses (@Shaggy)

-3 bytes: Use .replace() instead of .match().map() (@ThePirateBay)


(Go)Ruby, 111 bytes

x=0
c=1,1i,-1,-1i
gs.sc(/(\w)(\d*)/){|o,a|x+=c.ro!('URDL'.ix(o)||'NESW'.ix(o)+c.ix(1)).fs*a.toi}
p Mh::hy *x.rc

Try it online!

Takes input on STDIN, outputs it on STDOUT.

Basically, this approach uses complex numbers to store the current position, as well as a stack (c), containing offsets for each direction. If a direction is in URDL, the stack is rotated by the index of the direction in that string; if the direction is in NESW, it's rotated by the index of the direction in that string, plus the index of 1 in the stack. This transforms a rotation relative to the current position into a rotation relative to the position of 1. In any event, the top of the stack is multiplied by the number of steps in the direction and added to the current position.

Tags:

Code Golf