Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 06:48
Show Gist options
  • Save hassan-maavan/08ba006f6a4517dbcc00f8a277358bda to your computer and use it in GitHub Desktop.
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…
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