Part of Advent of Code Golf 2021 event. See the linked meta post for details.
The story continues from AoC2017 Day 11.
Obligatory why me and not Bubbler link
After having rescued a child process lost on a hexagonal infinite grid, you hear someone else screaming for help. You turn around, and unsurprisingly, there is another program looking for its own child process. "Help! It's gotten lost in an infinite octagonal grid!"
Well, it's not all octagonal, obviously. Instead, it's actually a 4-8-8 tiling:
An octagonal tile (X) has eight neighbors, indicated by eight directions (N, NE, E, SE, S, SW, W, NW). A square tile (Y) has only four neighbors in cardinal directions (N, E, S, W).
The program gives you the path taken by the child process. The initial tile is an octagon. You try following the directions one by one, and ... something's wrong in the middle. "Look, it can't move diagonally from a square tile, you see?"
Given a sequence of movements, determine if it is valid on the 4-8-8 grid, assuming the initial position of an octagon.
Input: A list of strings entirely consisting of N
, NE
, E
, SE
, S
, SW
, W
, NW
. Or a single string containing these strings with a single delimiter (space, comma, newline, or any other char that is not one of NESW
) in between. (If the input is ["N", "E", "NW", "NE", "E", "SE"]
then you can take it as e.g. "N,E,NW,NE,E,SE"
)
Output: A value indicating whether it is valid or not. You can choose to
- output truthy/falsy using your language's convention (swapping is allowed), or
- use two distinct, fixed values to represent true (affirmative) or false (negative) respectively.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
Truthy:
[]
["S"]
["SW"]
["N", "E", "NW", "NE"]
["NE", "SE", "SW", "NW", "N", "E", "S", "W"]
Falsy:
["N", "E", "NW", "NE", "E", "SE"]
["NE", "SE", "N", "E", "S", "SW", "NW", "W"]
["N", "NW"]
xx x xx xx x x xx xx x
or something? \$\endgroup\$,
be allowed as delimiter? It is not a single character but arguably a single delimiter. \$\endgroup\$