Skip to content

Instantly share code, notes, and snippets.

@yskooo
Created August 6, 2023 08:01
Show Gist options
  • Save yskooo/86becedf3543b38c60220e1ba947ac76 to your computer and use it in GitHub Desktop.
Save yskooo/86becedf3543b38c60220e1ba947ac76 to your computer and use it in GitHub Desktop.
Codewars: Kata Training() - 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 a…
export function isValidWalk(walk: string[]) {
const directions: { [key: string]: number } = {
n: 0,
s: 0,
w: 0,
e: 0,
};
walk.forEach((el) => (directions[el] += 1));
return walk.length === 10 && directions.n === directions.s && directions.w === directions.e;
}
// Example usage:
const walk1 = ['n', 's', 'e', 'w', 'n', 's', 'e', 'w', 'n', 's'];
console.log(isValidWalk(walk1)); // Output: true
const walk2 = ['n', 's', 'e', 'w', 'n', 's', 'e', 'w', 'n', 'w'];
console.log(isValidWalk(walk2)); // Output: false
w: 0,
e: 0,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment