Python 2.7 - 197 192 bytes
q='NESW';x,y,d=raw_input().split(',');x=int(x);y=int(y);d=q.find(d);v={0:'y+',1:'x+',2:'y-',3:'x-'}
for c in raw_input():exec['d+','d-',v[d]]['RL'.find(c)]+'=1;d=d%4'
print`x`+','+`y`+','+q[d]
I'm actually super proud of this one.
Explanation
First, let's clean up this mess. I used semicolons instead of line breaks in a lot of places because I think it makes me cool. Here it is normally (this is still 197 bytes, it hasn't been ungolfed at all). Yes, there's still a semicolon, but that one actually saves a byte.
q='NESW'
x,y,d=raw_input().split(',')
x=int(x)
y=int(y)
d=q.find(d)
v={0:'y+',1:'x+',2:'y-',3:'x-'}
for c in raw_input():m=v[d];exec['d+','d-',m]['RL'.find(c)]+'=1;d=d%4'
print`x`+','+`y`+','+q[d]
Let's begin!
q='NESW'
First we define q
as the string 'NESW'
. We use it twice later, and len("q='NESW';qq") < len("'NESW''NESW'")
.
x,y,d=raw_input().split(',')
Here we split the first line of inpupt at each comma. Our x coord is stored in x
, y in y
, and the first letter of our direction in d
.
x=int(x)
y=int(y)
Then we just make the coords ints. (I was shocked that I couldn't think of a better way to convert two strings to ints. I tried x,y=map(int,(x,y))
but that turns out to be longer.)
d=q.find(d)
This converts our direction to an integer. 0 is north, 1 is east, 2 is south and 3 is west.
v={0:'y+',1:'x+',2:'y-',3:'x-'}
This is where the fun starts.
When we go north, Y increases by 1. So this dictionary takes 0 and gives the string 'y+'
, for "increase y". It gives similar results for other directions: y or x followed by + or -.
We'll come back to this.
for c in raw_input():
m=v[d]
exec['d+','d-',m]['RL'.find(c)]+'=1;d=d%4'
I've gone to the liberty of ungolfing this one slightly.
For each character in the second line of input, we do two things. First, we set the variable m
to whatever our dictionary from before gives us for our current direction. There's no reason we need this to happen every time, but it's easier than just making it happen when we need it.
Next, we create a list with three elements: 'd+'
, 'd-'
, and m
. EDITOR'S NOTE: I think I can get away with not using the variable m
at all. I think I can just put v[d]
in the list directly. That'll save me a couple bytes if it works, but I don't feel like testing it until I'm done this explanation so y'all can deal. (Yep, it worked.)
We look for the current character of the input in the string 'RL'. str.find
returns -1 if it doesn't find the character, so this converts an R to a 0, an L to a 1 and anything else to -1. Of course, the only other input we can have is M, but it's less characters to make it work for everything.
We use that number as the index for the list we created. Python list indices start at the end if they're negative, so we get the first element if the input is R, the second if it's L, and the last if it's M. For for convenience's sake, I'm about to assume that we're facing north, but a similar principle applies for other directions.
The possible values we're working with are 'd+'
for R, 'd-'
for L and 'y+'
for M. Then, we attach '=1;d=d%4'
to the end of each one. That means our possible values are...
d+=1;d=d%4
d-=1;d=d%4
y+=1;d=d%4
That's valid python code! That's valid python code that does exactly what we want to do for each of those input characters! (The d=d%4
part just keeps our directions sane. Again, don't need to do it every time, but it's less characters.)
All we have to do is execute the code we get for each character, print it out (converting our direction back to a string), and we're done!