Skip to content

Instantly share code, notes, and snippets.

@kevincharm
Created December 11, 2017 12:32
Show Gist options
  • Save kevincharm/ef82440ab61f0c0a1c9c0c75532ff31a to your computer and use it in GitHub Desktop.
Save kevincharm/ef82440ab61f0c0a1c9c0c75532ff31a to your computer and use it in GitHub Desktop.
// -*- 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