Created
December 11, 2017 12:32
-
-
Save kevincharm/ef82440ab61f0c0a1c9c0c75532ff31a 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
// -*- node.jz -*- | |
let input = '' | |
process.stdin.on('readable', () => input += process.stdin.read() || '') | |
process.stdin.on('end', () => { | |
main() | |
}) | |
const dist = (i, j) => Math.abs(i) + Math.abs(j) | |
function main() { | |
const dirs = input.split(',') | |
let i = 0 | |
let j = 0 | |
let max = 0 | |
dirs.forEach(dir => { | |
const d = dir.split('') | |
const inc = 1/d.length | |
d.forEach(x => { | |
switch (x) { | |
case 'n': | |
j += inc | |
break | |
case 's': | |
j -= inc | |
break | |
case 'e': | |
i += inc | |
break | |
case 'w': | |
i -= inc | |
break | |
} | |
}) | |
max = Math.max(max, dist(i, j)) | |
}) | |
console.log(`total steps: ${dist(i, j)}`) | |
console.log(`max steps: ${max}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment