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
N
orS
:- If the character after the
N
orS
is anotherN
orS
:- Consume only the first
N
orS
. - Output
[0,1]
forN
- Output
[0,-1]
forS
- Consume only the first
- If the character after the
N
orS
isW
orE
:- Consume both the
N
orS
and theW
orE
. - Output
[1,1]
or[-1,1]
forNE
andNW
, respectively. - Output
[1,-1]
or[-1,-1]
forSE
andSW
, respectively.
- Consume both the
- If the character after the
- If the character is an
E
orW
not preceded by anS
orN
:- Consume the
E
orW
. - 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,
NWE
is 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\$ – Lynn Apr 24 '18 at 20:491
,-1j
,(-1+1j)
etc a valid output format? \$\endgroup\$ – Lynn Apr 24 '18 at 22:34NE
is justN+E
shouldn't it not matter? \$\endgroup\$ – Wheat Wizard Apr 25 '18 at 15:08