1 of 3

Bash/SHELF, 243

"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



for x in `Y . '& '`;do
d $x R&&o=`y NESW ESWN<<<$o`
d $x L&&o=`y NESW WNES<<<$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 \  ,`
p $o


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 a variable
for x in `Y . '& '`;do       #add a space after every char on stdin, then loop for each one
d $x R&&o=`y NESW ESWN<<<$o` #if it is R, change $o to turn right
d $x L&&o=`y NESW WNES<<<$o` #similarly for L
d $x M&&{                    #if it is M,
z=(`y , \ <<<$o`)            #replace commas with spaces and convert to array
case ${z[2]} in              #switch...case statement
#adjust the coordinates in the array, depending on where the rover is facing 
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
#turn the data back into a comma-separated string
o=`P ${z[@]}|y \  ,`
done #end loop
p $o #print output