Skip to content

Instantly share code, notes, and snippets.

@benfletcher
Created February 2, 2017 16:07
Show Gist options
  • Save benfletcher/a3431f415946e7980fbe80aeb5df462a to your computer and use it in GitHub Desktop.
Save benfletcher/a3431f415946e7980fbe80aeb5df462a to your computer and use it in GitHub Desktop.
const dir = {
North: { x: 0, y: 1, R: "East", L: "West" },
West: { x: -1, y: 0, R: "North", L: "South" },
South: { x: 0, y: -1, R: "West", L: "East" },
East: { x: 1, y: 0, R: "South", L: "North" }
};
const moves = {
R: ([x, y, d]) => ([x, y, dir[d].R]),
L: ([x, y, d]) => ([x, y, dir[d].L]),
A: ([x, y, d]) => ([x + dir[d].x, y + dir[d].y, d])
};
let start = [7, 3, "North"];
let input = "RAALALA";
let result = input.split("").reduce((pos, lra) => moves[lra](pos), start);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment