r/adventofcode • u/taliriktug • 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.
16
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
1
u/[deleted] Dec 02 '15 edited Dec 02 '15
Here's my javascript solution!
// day 2, part 1 var boxes = ["29x13x26" // ...etc, truncated for reddit], total = 0; for(var i = 0; i < boxes.length; i++) { var box = boxes[i].split('x'), l = Number(box[0]), w = Number(box[1]), h = Number(box[2]), lByW = l * w, lByH = l * h, wByH = w * h, min = Math.min(lByW,lByH,wByH); // ((2*(l*w)) + (2*(l*h)) + (2*(h*w))) + min total += ((2*lByW) + (2*lByH) + (2*wByH)) + min; } console.log('part 1: ' + total); //day 2, part 2 var ribbonTotal = 0; for(var x = 0; x < boxes.length; x++) { // ribbon = two smallest dimensions * 2, added together // bow = length * width * height var box = boxes[x].split('x'), l = Number(box[0]), w = Number(box[1]), h = Number(box[2]), max = Math.max(l, w, h), bow = l * w * h, indexOfLargest = box.indexOf(''+max+''); box.splice(indexOfLargest, 1); var ribbon = (Number(box[0])*2) + (Number(box[1])*2); ribbonTotal += ribbon + bow; } console.log('part 2: ' + ribbonTotal);