Where am I now?
Given a string d, containing only the letters NSWE, determine the coordinates I've traveled (from left to right, consuming greedily) and the final coordinate where I reside.
The rules for reading coordinates from left-to-right:
- If the next character is
NorS:- If the character after the
NorSis anotherNorS:- Consume only the first
NorS. - Output
[0,1]forN - Output
[0,-1]forS
- Consume only the first
- If the character after the
NorSisWorE:- Consume both the
NorSand theWorE. - Output
[1,1]or[-1,1]forNEandNW, respectively. - Output
[1,-1]or[-1,-1]forSEandSW, respectively.
- Consume both the
- If the character after the
- If the character is an
EorWnot preceded by anSorN:- Consume the
EorW. - Output
[1,0]forE. - Output
[-1,0]forW.
- Consume the
Worked Example
NSWE
[0,1] (North N) [-1,-1] (South-west SW) [1,0] (East E) [0,0] (N+SW+E = Didn't actually move) Note this can be in any format, here are other examples of valid output:
[[0,1],[-1,-1],[1,0],[0,0]] [[[0,1],[-1,-1],[1,0]],[0,0]] "0,1\n0,-1\n-1,0\n1,0\n0,0" Etc...
More Examples
SWSENNESWNE
[-1,-1] [1,-1] [0,1] [1,1] [-1,-1] [1,1] [1,0] NNEESESSWWNW
[0,1] [1,1] [1,0] [1,-1] [0,-1] [-1,-1] [-1,0] [-1,1] [0,0] NENENEE
[1,1] [1,1] [1,1] [1,0] [4,3] NEN
[1,1] [0,1] [1,2] EEE
[1,0] [1,0] [1,0] [3,0] Rules
- You may output in any convenient format that doesn't violate loopholes.
- You must consume greedily,
NWEis neverN,W,E, it is alwaysNW,E.- This applies to:
SW*,SE*,NW*,NE*. - You are consuming left to right, greedily.
- This applies to:
- The is code-golf, lowest byte-count wins.
[4, 3]or so would make it a little easier to see what's going on in the test output. \$\endgroup\$1,-1j,(-1+1j)etc a valid output format? \$\endgroup\$NEis justN+Eshouldn't it not matter? \$\endgroup\$