Created
December 15, 2017 15:22
-
-
Save mordaha/d96b0a0be9d58014e95a51e208f8c8a7 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
// 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