Turn me 22.5 with a rose

39

0

Given a string that is one of the directions on a 16-point compass rose

16-point compass rose

output the two directions that are immediately adjacent to the input direction, in clockwise order.

Specifically, you need to handle these (and only these) input/output pairs:

Input  Output
N      NNW NNE
NNE    N NE
NE     NNE ENE
ENE    NE E
E      ENE ESE
ESE    E SE
SE     ESE SSE
SSE    SE S
S      SSE SSW
SSW    S SW
SW     SSW WSW
WSW    SW W
W      WSW WNW
WNW    W NW
NW     WNW NNW
NNW    NW N

The output may be a string with some delimiter (not nothing) between the directions or a two-element list. The direction immediately counterclockwise to the input must appear first. You may use lowercase letters for the directions instead of uppercase but keep all input and output in one case or the other.

For example, for input N (or n if you're using lowercase) some valid outputs are:

NNW NNE
NNW-NNE
["NNW", "NNE"]
nnw-nne (if using lowercase)

Some invalid outputs are:

NNWNNE
NNE NNW
nnwNNE
NNw NNe

The shortest code in bytes wins.

Calvin's Hobbies

Posted 2017-04-14T17:02:19.030

Reputation: 84 000

Answers

12

Jelly, 37 34 bytes

“¢ ¬9£Hæz¥{çb¤S®!‘ṃ€“¡&¦»
¢iµ’,‘ị¢

Try it online!

Takes lowercase input.

-2 thanks to Jonathan Allan.
-1 since it turns out this is valid as a function :)

Thanks to Jonathan Allan (and Dennis), now you can remove the . Unfortunately, that would be non-competing here.

Detailed algorithm explanation:

We usually start explaining from the bottom (main) link, going down, but here I feel like it's more appropriate to explain from the top.

First, we simply load up the list [1, 32, 7, 57, 2, 67, 17, 92, 3, 94, 19, 119, 4, 109, 9, 34]. This looks like random numbers huh? Well, this is actually a list of base-5-compressed numbers, so we base-5-decompress it. Now it looks like [[1], [1, 1, 2], [1, 2], [2, 1, 2], [2], [2, 3, 2], [3, 2], [3, 3, 2], [3], [3, 3, 4], [3, 4], [4, 3, 4], [4], [4, 1, 4], [1, 4], [1, 1, 4]]. Still random-looking stuff, but this is actually an NESW-mapped list of the sixteen coordinates, so we're not far away from completing the list (Jelly is 1-indexed). Doing the final mapping, we get [['N'], ['N', 'N', 'E'], ['N', 'E'], ['E', 'N', 'E'], ['E'], ['E', 'S', 'E'], ['S', 'E'], ['S', 'S', 'E'], ['S'], ['S', 'S', 'W'], ['S', 'W'], ['W', 'S', 'W'], ['W'], ['W', 'N', 'W'], ['N', 'W'], ['N', 'N', 'W']], which is the complete list we want (Jelly strings are in the form [char1, char2, char3, ...].)

Since we've now built the coordinate list, we work with it. The main link comes into play. First, we load up the list we've built, and then take the index that the input (as command-line argument) coordinate resides in. Then, we pair its predecessor and its successor into a list, and we use them as modular indices into the same list of coordinates to take the coordinate to the left and right of the input respectively. You'd now think we're finally done, but there's in fact one more thing, the separator. This is valid as a function, since 1) You can call it using <integer>Ŀ 2) You're allowed to define other functions as well (like importing modules). Now, we are done. As a full program, this doesn't have a separator, but that's OK, since it works as a function.

Link-by-link code explanation:

¢iµ’,‘ị¢K Main link. Arguments: z = cmd0
¢         Run the helper link niladically (i.e. load the coordinate list).
 i        Find the index of z in the list.
  µ       Start a new monadic chain. Arguments: z = list_index.
   ’      Decrement z.
     ‘    Increment z.
    ,     Pair x and y into [x, y].
       ¢  Run the helper link niladically.
      ị   Take the elements of y at the indices in x.

“¢ ¬9£Hæz¥{çb¤S®!‘ṃ€“¡&¦» Helper link. Arguments: [1, 32, 7, 57, 2, 67, 17, 92, 3, 94, 19, 119, 4, 109, 9, 34]
“¢ ¬9£Hæz¥{çb¤S®!‘        Generate the integer list (the argument).
                    “¡&¦» Literal "newsy".
                  ṃ€      Base-length(y)-decompress every integer in x, then index into y.

Erik the Outgolfer

Posted 2017-04-14T17:02:19.030

Reputation: 38 134

Let us continue this discussion in chat.

– Erik the Outgolfer – 2017-04-15T17:48:12.773

11

Mathematica, 118 112 bytes

Thanks to Martin Ender for saving 6 bytes!

r=StringSplit@"N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW N NNE";<|Array[r[[#]]->r[[#+{-1,1}]]&,16,2]|>

Unnamed function (an association, really) that takes a string as input and returns an ordered pair of strings. Basically just hardcodes the answer.

Greg Martin

Posted 2017-04-14T17:02:19.030

Reputation: 13 940

8

Python 2, 116 115 103 bytes

-12 bytes thanks to Neil

d='N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW'.split()
n=d.index(input())
print d[n-1],d[n-15]

Try it Online!

math junkie

Posted 2017-04-14T17:02:19.030

Reputation: 2 490

2Use d[n-15] to avoid the condition. – Neil – 2017-04-14T18:52:31.167

1By the way, there's an extraneous quote at the end of the code segment in your answer. I would put in an edit request myself, but the edit has to be at least six characters and this would only be one. – notjagan – 2017-04-14T19:17:58.910

1@Neil Thanks! Saved a lot of bytes :) – math junkie – 2017-04-14T20:42:00.030

1@notjagan Thanks for pointing that out. Fixed it – math junkie – 2017-04-14T20:42:21.550

8

JavaScript ES6, 106 102 bytes

p=>[(a=`N|NNE|NE|ENE|E|ESE|SE|SSE|S|SSW|SW|WSW|W|WNW|NW|NNW`.split`|`)[i=a.indexOf(p)-1&15],a[i+2&15]]

Try it online!

const f = p=>[(a=`N|NNE|NE|ENE|E|ESE|SE|SSE|S|SSW|SW|WSW|W|WNW|NW|NNW`.split`|`)[i=a.indexOf(p)-1&15],a[i+2&15]]

console.log(f('N'))
console.log(f('NNE'))
console.log(f('ENE'))
console.log(f('E'))
console.log(f('ESE'))
console.log(f('SE'))
console.log(f('SSE'))
console.log(f('S'))
console.log(f('SSW'))
console.log(f('SW'))
console.log(f('WSW'))
console.log(f('W'))
console.log(f('WNW'))
console.log(f('NW'))
console.log(f('NNW'))

powelles

Posted 2017-04-14T17:02:19.030

Reputation: 1 277

Save 2 bytes with let instead of const. – HonoredMule – 2017-04-14T20:01:28.523

1

-4 bytes by moving variable declarations to where they are first used, Try it Online

– fəˈnɛtɪk – 2017-04-14T23:27:23.803

7

Javascript - 234 154 156 152 120 106 102 bytes

Only my second time doing code golf!!

Latest Revision:

Thank you to @fəˈnɛtɪk for this neat variable trick!

s=>[(a=`NNW N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW`.split` `)[n=a.indexOf(s)-1&15],a[n+2&15]]

Before That: Okay so latest revision: Input is a string and output is a string which is in the rules, so I made it into a function, and with reductions I have gone even smaller (also function is anonymous, which now means mine has somehow meshed into the other js answer oops! He (powelles) had it first!!):

(s,a=`NNW N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW`.split` `,n=a.indexOf(s))=>[a[n-1&15],a[n+1&15]]

Can be used by:

f=(s,a=`NNW N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW`.split` `,n=a.indexOf(s))=>[a[n-1&15],a[n+1&15]]
alert(f(prompt()))

Remade (not function) with Output - 120:

a="NNW N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW".split(' ');n=a.indexOf(prompt());alert(a[n-1&15]+' '+a[n+1&15]);
  • Note that I made an error originally, having it equal a.length instead of a.length-1 for the first index. Thanks @Neil for pointing out that it didn't work for NNW.

  • Note 2: Thank you to @Neil and @ETHProductions for helping me shorten the code!

Originial:

a="NNWN  NNENE ENEE  ESESE SSES  SSWSW WSWW  WNWNW ";l=prompt("","");n=l.length<3?l.length<2?l+'  ':l+' ':l;f=a.indexOf(n);i=f-3;j=f+3;i=i<0?a.length+--i:i;j=j+3>=a.length?j-a.length:j;alert(a.substring(i,i+3)+' '+a.substring(j,j+3));

Blue Okiris

Posted 2017-04-14T17:02:19.030

Reputation: 157

1Welcome back to golfworld! – Greg Martin – 2017-04-14T17:47:53.957

1This doesn't work for NNW. – Neil – 2017-04-14T18:43:46.453

@Neil You are correct. I will fix it! – Blue Okiris – 2017-04-14T19:32:20.073

@Neil It is fixed – Blue Okiris – 2017-04-14T19:34:40.967

@ETHproductions I was just about to fix that ;) But thank you anyway! – Blue Okiris – 2017-04-14T19:39:06.903

You can also call prompt() with no arguments and it will be exactly the same ;) – ETHproductions – 2017-04-14T19:42:13.850

Also, n-1>-1 = n>0 – ETHproductions – 2017-04-14T19:43:44.483

Or just n, as we can assume that the value is always in the list. – Neil – 2017-04-14T19:44:36.263

Actually as the list is always 16 long, you can use n-1&15 and n+1&15. – Neil – 2017-04-14T19:45:51.960

1I was working towards my own solution before I realised it was very similar to yours. A few tips for you: 1) Anonymous functions are valid, 2) You don't need to request an input in your submission, it just needs to be able to receive one, 3) You don't need to log the output in your submission, simply return. With all that in mind, here's the 106 characters I was down to for you to improve your solution with: p=>(a="N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,NNW".split,,i=a.indexOf(p),[a[i-1&15],a[i+1&15]]) – Shaggy – 2017-04-14T20:14:12.007

And here is a working Fiddle. I was just about to start into trying to optimise the population of the array so would love to hear any suggestions for that.

– Shaggy – 2017-04-14T20:14:54.423

1

-4 bytes by moving variable declarations into where they are used, Try it Online

– fəˈnɛtɪk – 2017-04-14T23:25:06.670

7

05AB1E, 44 43 bytes (Thanks to Adnan)

"ESNW4"•2ßU^]>Þ‡¾“¾&é{½‡•5BSè4¡©skD<®ès>®è)

Try it online!

"ESNW4"•2ßU^]>Þ‡¾“¾&é{½‡•5BSè # Push N4NNE4NE4ENE4E4ESE4SE4SSE4S4SSW4SW4WSW4W4WNW4NW4NNW
4¡©            # Split on 4's and store.
   sk          # Index of input in direction array.
     D<®è      # Element before index of input.
         s>®è  # Element after index of input.
             ) # Wrap two element to array.

Exmaple output:

N => [NNW,NNE]

Version that pushes N0NNE0NE0ENE0E0ESE0SE0SSE0S0SSW0SW0WSW0W0WNW0NW0NNW instead:

•17¿$Mn]6VAÆ—Dªd—•5B4LJ"NSWE"‡0¡©skD<®ès>®è)

Is also 44-bytes, there was 0 reason for my refactor and there is 0 reason for splitting on the 4's.


Magic Octopus Urn

Posted 2017-04-14T17:02:19.030

Reputation: 19 422

1Any special reason to split on 4s? – Greg Martin – 2017-04-14T17:48:22.443

@GregMartin •17¿$Mn]6VAÆ—Dªd—•5B4LJ"NSWE"‡0¡©skD<®ès>®è) turns out that, no, there is no reason at all. Using 0 as the delimiter is the same compression ratio, as it doesn't drop the length of the number in the base-5 conversion to base-214. Coulda sworn doing it like that saved me a byte though. – Magic Octopus Urn – 2017-04-14T18:33:28.690

You can do „ €Ã¦•174SÝ©l2ÎG¦˜fÐ98•5BSè#ÐIk©<ès®>è) to save 4 bytes. – Emigna – 2017-04-16T08:54:16.153

3

Jelly,  40 38 bytes

“NSWE”“dḍ.ƈ€ḶƘfƥ’ṃṁ
“¢)`)’ḃ3RÇṙi¥µṖṪ,Ḣ

Try it online! (added the footer to show the output is a list of two items) ...or see all cases.

(I'm not quite sure why 1323DRẋ4 in place of “¢)`)’ḃ3R doesn't work at the moment.)

How?

“NSWE”“dḍ.ƈ€ḶƘfƥ’ṃṁ - Link 1, rose list helper: shape array
“NSWE”              - "NSWE"
      “dḍ.ƈ€ḶƘfƥ’   - base 250 number: 1554210846733274963415
                 ṃ  - base decompress: "NNNENEENEEESESESSESSSWSWWSWWWNWNWNNW"
                  ṁ - mould like shape array

“¢)`)’ḃ3RÇṙi¥µṖṪ,Ḣ - Main link: direction string
“¢)`)’             - base 250 number: 33899292
      ḃ3           - to base 3: [1,3,2,3,1,3,2,3,1,3,2,3,1,3,2,3]
        R          - range (vectorises) [[1],[1,2,3],[1,2],[1,2,3],[1],[1,2,3],[1,2],[1,2,3],[1],[1,2,3],[1,2],[1,2,3],[1],[1,2,3],[1,2],[1,2,3]]
         Ç         - call the last link (1) as a monad: ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
            ¥      - last two links as a dyad
          ṙ        -     rotate the list by:
           i       -         index of the direction string in the list
             µ     - monadic chain separation (call the rotated list x)
              Ṗ    - pop (x[:-1]) (removes the direction string)
               Ṫ   - tail that (i.e. x[-2])
                 Ḣ - head x (i.e. x[0])
                ,  - pair

Jonathan Allan

Posted 2017-04-14T17:02:19.030

Reputation: 67 804

3

Batch, 196 bytes

@set s=N
@if %1==N echo NNW
@for %%r in (NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW)do @call:c %1 %%r
@if %1==NNW echo N
@exit/b
:c
@if %1==%2 echo %s%
@if %1==%s% echo %2
@set s=%2

Loops through each pair of compass points, printing one when the other matches. For example, for a parameter of ENE, when the loop reaches ENE, the variable s contains NE which is printed, then when the loop advances to E, the variable s contains ENE and so E is printed. One pair then needs to be special-cased to avoid the compass points from being printed in the wrong order.

Neil

Posted 2017-04-14T17:02:19.030

Reputation: 95 035

3

Haskell, 100 99 bytes

s=words"N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW"++s
(a:b:c:r)#x|x==b=(a,c)|1<3=r#x
(s#)

Try it online! Calling (s#) "N" returns ("NNW","NNE").

s is an infinite repetition of the list of directions, thus we don't have to add an extra N and NNE like some of the other answers to correctly handle the edges of the list.

Thanks to @nimi for saving one byte!

Laikoni

Posted 2017-04-14T17:02:19.030

Reputation: 23 676

1An infix function saves a byte: (a:b:c:r)!x| ... =r!x;(s!). – nimi – 2017-04-15T01:12:37.977

2

SOGL, 33 bytes

≠┐πΜ]ρ½d⁹V¹-┐*╔╤¹Ψæ;¶‘θ,W:AHwOaIw

The first part ≠┐πΜ]ρ½d⁹V¹-┐*╔╤¹Ψæ;¶‘ is a compressed string that is

N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW

compressed with a custom dictionary with ENSW

The rest of the program:

...‘θ,W:AHwOaIw  example input: NNW
...‘             push the compressed string        ["N NNE NE ... NNW"]
    θ            split on spaces                   [["N","NNE",...,"NNW"]]
     ,W          get the index of input            [["N","NNE",...,"NNW"], 16]
       :A        save the index on variable A      [["N","NNE",...,"NNW"], 16]
         H       decrease [the index]              [["N","NNE",...,"NNW"], 15]
          wO     output that'th item of the array  [["N","NNE",...,"NNW"]]
            a    load variable A                   [["N","NNE",...,"NNW"], 16]
             I   increase [the index]              [["N","NNE",...,"NNW"], 17]
              w  get that item in the array        [["N","NNE",...,"NNW"], "N"]

dzaima

Posted 2017-04-14T17:02:19.030

Reputation: 19 048

What code page? – Joshua – 2017-04-15T20:38:32.760

@Joshua The bytes in the title has a link to the codepage – dzaima – 2017-04-15T20:39:51.597

@Joshua Actually, that was missing a couple of characters because of markdown , but it's fixed now – dzaima – 2017-04-15T21:25:54.177

2

PHP, 122 bytes

preg_match("/([^ ]+ )$argv[1] ([^ ]+)/",'N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW N NNE',$m);
echo $m[1].$m[2];

kld87

Posted 2017-04-14T17:02:19.030

Reputation: 21

1You can save 2 bytes removing unnessary whitespaces. -3 bytes for replacing $argv[1] with $argn and using the -R option. if you use deprecated functions if could be ending in ereg("([^_]+)_{$argn}(_[^_]+)",N_NNE_NE_ENE_E_ESE_SE_SSE_S_SSW_SW_WSW_W_WNW_NW_NNW_N_NNE,$t);echo$t[1].$t[2]; – Jörg Hülsermann – 2017-04-18T13:22:12.483

1

PHP, 115 Bytes

for($r=explode(_,($w=N_NNE_NE_ENE_E_ESE_SE_SSE_).strtr($w,SWNE,NESW).$w);$r[++$i]!=$argn;);echo$r[$i-1]._.$r[$i+1];

-2 Bytes using the deprecated function split instead of explode

PHP, 128 Bytes

for($i=2;$i--;print$i?end($e)._:$e[2])$e=explode(_,strstr(($w=N_NNE_NE_ENE_E_ESE_SE_SSE_).strtr($w,SWNE,NESW).$w,_.$argn._,$i));

PHP, 134 Bytes

echo($r=explode(_,N_NNE_NE_ENE_E_ESE_SE_SSE_S_SSW_SW_WSW_W_WNW_NW_NNW))[($k=array_search($argn,$r))-1<0?15:$k-1]._.$r[$k+1>15?0:$k+1];

Jörg Hülsermann

Posted 2017-04-14T17:02:19.030

Reputation: 13 026

1

Ruby - 94 Bytes

A riff on Blue Okiris's answer, just to take advantage of some nice Ruby shorthand (the %w[] syntax and p specifically):

->(s,d=%w[N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW],n=d.index(s)){p d[n-1],d[n-15]}

gntskn

Posted 2017-04-14T17:02:19.030

Reputation: 243

1

Japt, 66 52 bytes

Saved 14 bytes thanks to @ETHproductions

V=`ã@JaÀTeaÀÄsÁÁss°s°ws°°wn°n°nnw`qa [J1]£VgX+VaU

Try it online!

Explanation:

V=(`...`qa) [J1]£Vg(X+VaU)
V=(       )                // Set V to:
   `...`                   //   "nanneaneaeneaeaeseaseasseasasswaswawswawawnwanwannw" (de-)compressed
        qa                 //    Split on "a", creating [n...nnw]
            [J1]           // [-1,1]
                £          // Iterate through ^, X becomes the iterative item
                 Vg(     ) //   V.Item at index:
                    X+VaU  //     X + V.indexOf(input)

Oliver

Posted 2017-04-14T17:02:19.030

Reputation: 7 160

Very nice. A couple improvements: 1) You can take the input in lowercase instead of converting the array to uppercase. 2) You can actually remove the ' in q'o and it will work exactly the same :-) – ETHproductions – 2017-04-15T14:14:28.633

Also, you can reduce the array construction at the end to [J1]£VgX+VaU to save a few bytes – ETHproductions – 2017-04-15T14:17:04.987

@ETHproductions That is brilliant, thanks! – Oliver – 2017-04-16T03:36:54.983

1

PHP, 110 109 bytes

Saved 1 byte thanks to Jörg Hülsermann.

echo@preg_filter("/.*?(\w+) $argn( \w+).*/",'$1$2',($s='N NNE NE ENE E ESE SE SSE ').strtr($s,NESW,SWNE).$s);

user63956

Posted 2017-04-14T17:02:19.030

Reputation: 1 571

2You could replace preg_replace with preg_filter to save 1 byte – Jörg Hülsermann – 2017-04-18T13:23:42.590

1

CJam, 41

"NeSWN"2ew{_1<\_La*\$f+~}%:eu_ra#(m<2%2<p

Try it online

aditsu quit because SE is EVIL

Posted 2017-04-14T17:02:19.030

Reputation: 22 326

0

Python 3 - 112 107 bytes

I based this off of my Javascript answer:

Remade:

lambda i,s="N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW".split():[s[s.index(i)-1],s[s.index(i)-15]]

Use as say

f = lambda i,s="N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW".split():[s[s.index(i)-1],s[s.index(i)-15]]
print(f(input()));

Original:

lambda i,s="N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW".split():[s[s.index(i)-1&15],s[s.index(i)+1&15]]

Blue Okiris

Posted 2017-04-14T17:02:19.030

Reputation: 157

0

MATL, 43 bytes

';evl(Z?&fWElf`gvhM'F' NESW'ZaYbtjY=fFTEq+)

Try it online!

Explanation

';evl(Z?&fWElf`gvhM' % Push this string
F                    % Push false
' NESW'              % Push this string
Za                   % Base conversion. This decompresses the first string from alphabet
                     % given by all printable ASCII except single quote to the alphabet
                     % ' NESW'. The result is the following string, which is pushed:
                     % 'N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW'
Yb                   % Split at spaces. Gives a cell array of strings
t                    % Duplicate
j                    % Input string
Y=                   % String comparison. Gives an array containing true at the index
                     % of the matching string
f                    % Find: index of the entry that equals true
FTEq                 % Push [-1 1] (obtained as [false true], times 2, minus 1)
+                    % Add, element-wise
)                    % Index modularly into the cell array of strings
                     % Implicitly display. Each cell is displayed on a different line

Luis Mendo

Posted 2017-04-14T17:02:19.030

Reputation: 87 464

0

c, 222 216 211 bytes

main(c,v)char**v;{char r[]="NXNNEXNEXENEXEXESEXSEXSSEXSXSSWXSWXWSWXWXWNWXNWXNNWX",*p=r,*s=r,*a[16],i=0,n;for(;*p++;)*p==88?*p=0,n=strcmp(a[i++]=s,v[1])?n:i-1,s=++p:0;printf("%s %s\n",a[n?n-1:15],a[n<15?n+1:0]);}

Try it online

Johan du Toit

Posted 2017-04-14T17:02:19.030

Reputation: 1 524

0

Javascript (ES6), 189 bytes

d="N.NNW NNE.NNE.N NE.NE.NNE ENE.ENE.NE E.E.ENE ESE.ESE.E SE.SE.ESE SSE.SSE.SE S.S.SSE SSW.SSW.S SW.SW.SSW WSW.WSW.SW W.W.WSW WNW.WNW.W NW.NW.WNW NNW.NNW.NW N".split`.`,f=>d[d.indexOf(f)+1]

Just takes the input, looks it up, and returns it.

user68278

Posted 2017-04-14T17:02:19.030

Reputation:

0

JavaScript (ES6), 94 bytes

Expects a string in uppercase such as "ENE". Returns a comma separated string such as "NE,E".

s=>/\D+,\D+/.exec('NNW0N0NNE0NE0ENE0E0ESE0SE0SSE0S0SSW0SW0WSW0W0WNW0NW0NNW0N'.split(0+s+0))[0]

How it works

The expression 0+s+0 is coerced to a string when split() is called. For instance, if the input is "ENE", the string will be split on "0ENE0":

"NNW0N0NNE0NE0ENE0E0ESE0SE0SSE0S0SSW0SW0WSW0W0WNW0NW0NNW0N"
             ^^^^^

This leads to the following array:

[ "NNW0N0NNE0NE", "E0ESE0SE0SSE0S0SSW0SW0WSW0W0WNW0NW0NNW0N" ]

Again, this array is coerced to a string when exec() is called. So, the regular expression is actually applied on:

"NNW0N0NNE0NE,E0ESE0SE0SSE0S0SSW0SW0WSW0W0WNW0NW0NNW0N"

We look for consecutive non-numeric characters (\D+) followed by a comma, followed by consecutive non-numeric characters. This returns the array [ "NE,E" ]. We could arguably stop there and return just that. But the challenge is asking for either a delimited string or a two-element array. So, we extract the string with [0].

Demo

let f =

s=>/\D+,\D+/.exec('NNW0N0NNE0NE0ENE0E0ESE0SE0SSE0S0SSW0SW0WSW0W0WNW0NW0NNW0N'.split(0+s+0))[0]

;
["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
.map(
  s => console.log(s, '=>', f(s))
)

Arnauld

Posted 2017-04-14T17:02:19.030

Reputation: 111 334

0

Pyth, 39 bytes:

.rL]z_Bms@L"ESWN"jCd5"\"❤m❤w❤^❤\❤C❤9❤ ❤

where represents unprintable letters.

Try it online!

Hexdump:

0000000: 2e 72 4c 5d 51 5f 42 6d 73 40 4c 22 45 53 57 4e .rL]Q_Bms@L"ESWN
0000010: 22 6a 43 64 35 22 5c 22 09 6d 04 77 13 5e 03 5c "jCd5"\".m.w.^.\
0000020: 11 43 02 39 07 20 01                            .C.9. .

Leaky Nun

Posted 2017-04-14T17:02:19.030

Reputation: 45 011