14
3
Many of us are familiar with the game Tron. You control a "lightcycle" placed on a grid. The lightcycle always moves forward (though you control the direction) and leaves a permanent trail behind it. If you run into a trail, you crash!
The goal here is to determine if a given path is a valid loop, that is, it returns to its starting point without "crashing". To do this, we assume we start at the point (0,0)
. An input is given in the form N2E1S2W1
, with a series of cardinal directions (N
is north
, E
is east
, and so on), each followed by the distance to travel that direction. In this example, you would travel
N2 : North 2 to (0,2)
E1 : East 1 to (1,2)
S2 : South 2 to (1,0)
W1 : West 1 to (0,0)
A path is considered valid if it ends at (0,0)
without visiting any other coordinate more than once (it visits (0,0)
exactly twice. Once at the start, and once at the end). Keep in mind than in the above example, to get from (0,0)
to (0,2)
, we necessarily visit (0,1)
as well.
Other examples:
input -> output
N1E1S1W1 -> true
N1E1N1E1S2W2 -> true
N1S1E1W1 -> false // Visits (0,0) 3 times
N4E2S2W4S2E2 -> false // Visits (0,2) twice
N3E2S3 -> false // Does not return to (0,0)
N1S1 -> anything //I don't care how you evaluate this case
Your output can be in any form so long as it gives the same output for any truthy or falsey value.
The input can be taken as a string or as a list of characters, either in the form S1N2E3
... or SNNEEE
... There's is also no hard limit on the grid size, but assume the input isn't going to overflow anything. As long as the code is fundamentally sound, It's not crucial to handle cases like N99999999999999
.
NOTE: You may evaluate the cases N1S1
, E1W1
, S1N1
, and W1E1
however you would like. They're technically valid paths, but they go against the "Tron" spirit of the challenge.
Scoring
This is code-golf, so the shortest answer wins!
N1S1
should be true to be consistent with your definitions because it reaches(0, 0)
twice and(0, 1)
once, which is valid under your definition. – HyperNeutrino – 2017-07-17T19:34:05.113Can I take
N
as1j
,E
as1
,S
as-1j
, andW
as-1
? – Leaky Nun – 2017-07-17T19:35:58.883@LeakyNun I think I'm ok with that, since everyone should more or less be doing something like this the hood anyway. Just make sure you specify that in your answer. – Lord Farquaad – 2017-07-17T19:38:38.303
1@HyperNeutrino but from a Tron standpoint, your lightcycle goes through (0, 0.5) twice, even if the input will never put you at such a point. That's why I think that one has flexible output (even though for most languages it'll be easier to return true) – Value Ink – 2017-07-17T19:38:47.787
@ValueInk Okay, makes sense. – HyperNeutrino – 2017-07-17T19:39:27.473
Will 0,0 be in a corner, ie are we guaranteed to only get positive coordinates? And is there a grid size limit, or is it infinite? – steenbergh – 2017-07-17T19:56:32.450
1@steenbergh (0,0) is not in the corner, so you can go under or to the left of it; even both if you're feeling crazy! Also, there's no hard limit on the grid size, but just assume the input isn't going to overflow anything. As long as the code is fundamentally sound, I don't care if it can't handle inputs like
N99999999999999
– Lord Farquaad – 2017-07-17T20:00:53.020Is
N1E1N1E1S1W1S1W1
valid? – Leaky Nun – 2017-07-17T20:18:03.330@LeakyNun it is not. In a technical sense, you visit
(1,1)
twice. In a tron sense, our lightcycle has some width to it, so even though we only touch "corners", we'd still crash. – Lord Farquaad – 2017-07-17T20:20:05.957