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.
Let us continue this discussion in chat.
– Erik the Outgolfer – 2017-04-15T17:48:12.773