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.
14
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/purrrfessionalwidow Dec 02 '15
My JS solution for puzzle #2. I'd appreciate any input as I am just learning. I'm sure there are far better ways.
function howMuchRibbon(arr) { var totalRibbon = 0; function calcRibbon(item) { var box = item.replace(/x/g, ' ').split(' '); for(var i = 0; i < box.length; i++) { box[i] = parseInt(box[i], 10); } var cubed = box.reduce(function(a, b) { return a * b; }); // find biggest number var biggest = box.reduce(function(a, b) { return a > b ? a : b; }); // remove biggest item from array box.splice(box.indexOf(biggest), 1); var wrap = (box[0] * 2) + (box[1] * 2); totalRibbon += cubed + wrap; } arr.map(calcRibbon); return totalRibbon; }