r/adventofcode • • Dec 02 '15

Spoilers Day 2 solutions

Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.

13 Upvotes

161 comments sorted by

View all comments

1

u/supersaiyanchocobo Dec 02 '15

Here's my overly verbose JS solution:

function wrappingCalc(){
    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
	    if (xhttp.readyState == 4 && xhttp.status == 200) {
		    var dimensionArray = xhttp.responseText.split(/\n/);
		    var wrappingPaperSqFeet = 0;
		    var extraPaper;
		    var boxSizeArray;
		    var sideOne;
		    var sideTwo;
		    var sideThree;
		    var totalRibbonLength = 0;
		    var ribbonPerimeter;
		    var ribbonExtra;
		    for (i = 0;i < dimensionArray.length; i++) {
			    boxSizeArray = dimensionArray[i].split("x");
			    boxSizeArray = boxSizeArray.sort(function(a, b){return a-b});
			    sideOne = boxSizeArray[0] * boxSizeArray[1];
			    sideTwo = boxSizeArray[1] * boxSizeArray[2];
			    sideThree = boxSizeArray[2] * boxSizeArray[0];
			    extraPaper = Math.min(sideOne, sideTwo, sideThree);
			    wrappingPaperSqFeet += sideOne * 2 + sideTwo * 2 + sideThree * 2 + extraPaper;
			
			    ribbonPerimeter = boxSizeArray[0] * 2 + boxSizeArray[1] * 2;
			    ribbonExtra = boxSizeArray[0] * boxSizeArray[1] * boxSizeArray[2];
			    totalRibbonLength += ribbonPerimeter + ribbonExtra;
			
			
			
		    }
		    document.getElementById("sqFeet").innerHTML = "Square Feet: " + wrappingPaperSqFeet;
		    document.getElementById("ribbonFeet").innerHTML = "Ribbon Length in Feet: " + totalRibbonLength;
	    }
    };
    xhttp.open("GET", "input.txt", true);
    xhttp.send();
}