Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Advent of code 2015 - Day 2
function parseDimension(dimension) {
var packageDimensions = dimension.split("x");
return packageDimensions;
}
function numericSort(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
var input = document.getElementsByTagName("pre")[0].innerText;
var dimensions = input.split("\n");
var totalSquareFeet = 0;
var totalRibbion = 0;
for (i=0; i<dimensions.length-1; i++) {
var packageDimensions = parseDimension(dimensions[i]);
var l = packageDimensions[0];
var w = packageDimensions[1];
var h = packageDimensions[2];
totalSquareFeet += 2*l*w + 2*w*h + 2*h*l;
totalSquareFeet += Math.min(l*w,w*h,h*l);
var sortedDimensions = [l, w, h].map(item => item*1).sort(numericSort);
totalRibbion += sortedDimensions[0] + sortedDimensions[0];
totalRibbion += sortedDimensions[1] + sortedDimensions[1];
totalRibbion += l*w*h;
}
console.log("Total square feet : " + totalSquareFeet);
console.log("Total ribbion square feet : " + totalRibbion);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.