Skip to content

Instantly share code, notes, and snippets.

@reidblomquist
Created December 3, 2021 03:40
Show Gist options
  • Save reidblomquist/71efad347850a6219a97471ad585cf53 to your computer and use it in GitHub Desktop.
Save reidblomquist/71efad347850a6219a97471ad585cf53 to your computer and use it in GitHub Desktop.
still pretty schlopy day 2 advent
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