2 of 3 golfed; added 34 characters in body

# Bash/SHELF, 243 235

"SHEll goLF" is a golfing library for Bash which provides a few useful aliases. This is a valid answer as the library existed and was on GitHub before the challenge was posted.

## How to run

This takes the initial position (separated by commas as specified; this adds a lot of characters to the code) as its first argument, and the instructions on standard input.

source shelf.sh #you must load SHELF first
source rover.sh 1,2,N<<<MRMLM #now run the script via source so it has access to SHELF


## Sample output

2,4,N


## Code

o=$1 D(){ o=y NESW$1<<<$o;} for x in Y . '& ';{ d$x R&&D ESWN
d $x L&&D WNES d$x M&&z=(y , \ <<<$o)&&case${z[2]} in N) z[1]=$[z[1]+1];;S) z[1]=$[z[1]-1];;W) z[0]=$[z[0]-1];;E) z[0]=$[z[0]+1];;esac&&o=P ${z[@]}|y \ , } p$o


## Explanation

d is for comparison; it returns 0 if its two arguments are equal and 1 otherwise, it can then have other commands chained onto it with && and ||.

y is like tr (but done through sed).

Y is like sed 's/.../.../g' for its two arguments.

P is echo -e -n; p is just echo -e.

o=$1 #save first argument to variable D(){ o=y NESW$1<<<$o;} #define an alias to turn R or L for x in Y . '& ';{ #add a space after every character on stdin and loop for each one d$x R&&D ESWN #turn R using alias
d $x L&&D WNES #turn L using alias  The next bit is profoundly ugly, with about 145 chars on one line. If the current command is M, turn the commas in$o into spaces, convert to array and save to $z. Then, do a switch...case block for the last element of$z (the direction the rover is pointing. Change coordinates accordingly, then convert $z back into a comma-separated string and save to$o.

d $x M&&z=(y , \ <<<$o)&&case ${z[2]} in N) z[1]=$[z[1]+1];;S) z[1]=$[z[1]-1];;W) z[0]=$[z[0]-1];;E) z[0]=$[z[0]+1];;esac&&o=P${z[@]}|y \  ,
} #end loop
p \$o #print output

 answered Jun 19 '14 at 18:31 user16402