Skip to content

Instantly share code, notes, and snippets.

@Gregoor
Last active December 6, 2015 19:21
Show Gist options
  • Save Gregoor/14e03488aa457a42af34 to your computer and use it in GitHub Desktop.
Save Gregoor/14e03488aa457a42af34 to your computer and use it in GitHub Desktop.
Advent of Code - Day 2: I Was Told There Would Be No Math
const calcPaper = (l, w, h) => {
const sides = [l * w, w * h, h * l];
return Math.min(...sides) + sides.reduce((n, v) => n + 2 * v, 0);
};
const calcRibbon = (...dimensions) => {
const max = Math.max(...dimensions);
return 2 * dimensions.filter(n => n !== max).reduce((n, v) => n + v, 0) +
dimensions.reduce((n, v) => n * v, 1);
}
const expect = (actual, expected) => {
if (actual !== expected) {
console.error(`Expected ${expected} got ${actual}`);
return false;
}
return true;
}
console.clear()
const tests = [
...[
[[2, 3, 4], 58],
[[1, 1, 10], 43]
].map(([dimensions, expected]) => {
return expect(calcPaper(...dimensions), expected);
}),
...[
[[2, 3, 4], 34],
[[1, 1, 10], 14]
].map(([dimensions, expected]) => {
return expect(calcRibbon(...dimensions), expected);
})
];
if (tests.every(Boolean)) console.log('All tests passed!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment