Created
February 2, 2017 16:07
-
-
Save benfletcher/a3431f415946e7980fbe80aeb5df462a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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