Created
September 19, 2020 06:48
-
-
Save hassan-maavan/08ba006f6a4517dbcc00f8a277358bda to your computer and use it in GitHub Desktop.
You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter string…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValidWalk(walk) { | |
var dx = 0 | |
var dy = 0 | |
var dt = walk.length | |
for (var i = 0; i < walk.length; i++) { | |
switch (walk[i]) { | |
case 'n': dy--; break | |
case 's': dy++; break | |
case 'w': dx--; break | |
case 'e': dx++; break | |
} | |
} | |
return dt === 10 && dx === 0 && dy === 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment