Created
December 3, 2021 03:40
-
-
Save reidblomquist/71efad347850a6219a97471ad585cf53 to your computer and use it in GitHub Desktop.
still pretty schlopy day 2 advent
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 data = require('./data.json'); | |
const results = data.data.reduce((acc, val, idx, arr) => { | |
const [direction, rawValue] = val.split(' '); | |
const value = Number(rawValue); | |
switch (direction) { | |
case 'up': | |
acc.aim -= value; | |
break; | |
case 'down': | |
acc.aim += value; | |
break; | |
case 'forward': | |
acc.horizontalPosition += value; | |
acc.depth += (acc.aim * value); | |
break; | |
default: | |
break; | |
} | |
return acc; | |
}, { | |
horizontalPosition: 0, | |
depth: 0, | |
aim: 0, | |
}) | |
console.log(results.horizontalPosition * results.depth); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment