Created
December 1, 2016 22:09
-
-
Save Kattoor/bf5677fa2a60ab07cd1e7e9e92679b49 to your computer and use it in GitHub Desktop.
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
const position = { x: 0, y: 0 } | |
// left up right down | |
const Dir = [ [0, 3, 1, {x: -1, y: 0}], [1, 0, 2, {x: 0, y: 1}], [2, 1, 3, {x: 1, y: 0}], [3, 2, 0, {x: 0, y: -1}]] | |
let currentDirectionIndex = 1 | |
function move(input) { | |
const dir = input[0] | |
const steps = input.substr(1) | |
currentDirectionIndex = Dir.find(direction => Dir[currentDirectionIndex][dir === 'L' ? 1 : 2] === direction[0])[0] | |
let currentDirection = Dir[currentDirectionIndex] | |
position.x += currentDirection[3].x * steps | |
position.y += currentDirection[3].y * steps | |
} | |
const inputString = "L5, R1, L5, L1, R5, R1, R1, L4, L1, L3, R2, R4, L4, L1, L1, R2, R4, R3, L1, R4, L4, L5, L4, R4, L5, R1, R5, L2, R1, R3, L2, L4, L4, R1, L192, R5, R1, R4, L5, L4, R5, L1, L1, R48, R5, R5, L2, R4, R4, R1, R3, L1, L4, L5, R1, L4, L2, L5, R5, L2, R74, R4, L1, R188, R5, L4, L2, R5, R2, L4, R4, R3, R3, R2, R1, L3, L2, L5, L5, L2, L1, R1, R5, R4, L3, R5, L1, L3, R4, L1, L3, L2, R1, R3, R2, R5, L3, L1, L1, R5, L4, L5, R5, R2, L5, R2, L1, L5, L3, L5, L5, L1, R1, L4, L3, L1, R2, R5, L1, L3, R4, R5, L4, L1, R5, L1, R5, R5, R5, R2, R1, R2, L5, L5, L5, R4, L5, L4, L4, R5, L2, R1, R5, L1, L5, R4, L3, R4, L2, R3, R3, R3, L2, L2, L2, L1, L4, R3, L4, L2, R2, R5, L1, R2" | |
const commands = inputString.split(", ") | |
for (const command of commands) | |
move(command) | |
console.log("Distance: " + (Math.abs(position.x) + Math.abs(position.y))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment