Skip to content

Instantly share code, notes, and snippets.

@mordaha
Created December 15, 2017 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mordaha/d96b0a0be9d58014e95a51e208f8c8a7 to your computer and use it in GitHub Desktop.
Save mordaha/d96b0a0be9d58014e95a51e208f8c8a7 to your computer and use it in GitHub Desktop.
// http://adventofcode.com/2017/day/11
import data from './data.json'
const steps = data.split(',')
const pairs = steps.map(s => {
switch (s) {
case 'n':
return [2, 0]
case 'ne':
return [1, 1]
case 'se':
return [-1, 1]
case 's':
return [-2, 0]
case 'sw':
return [-1, -1]
case 'nw':
return [1, -1]
default:
throw Error(s)
}
})
const [north, east] = pairs.reduce(
(acc, p) => {
return [acc[0] + p[0], acc[1] + p[1]]
},
[0, 0],
)
const [absNorth, absEast] = [Math.abs(north), Math.abs(east)]
const stepDistance = absNorth > absEast ? (absNorth - absEast) / 2 + absEast : absEast
console.log('RESULT: ', north, east, stepDistance)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment