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.

15 Upvotes

161 comments sorted by

View all comments

Show parent comment

2

u/PM_ME_INSIDER_INFO Dec 03 '15

Just FYI instead of doing:

if (side1 <= side2 && side1 <= side3) {
  smallestSide = side1;
} else if (side2 <= side1 && side2 <= side3) {
  smallestSide = side2;
} else if (side3 <= side1 && side3 <= side2) {
  smallestSide = side3;
}

You could just do:

var dim = [l, w, h].sort(function (a, b) {
    return a - b;
});

And then you already know what the smallest, mid, and largest are.

1

u/aveavaeva Dec 03 '15

Sure that's smaller and simpler but I prefer to keep solutions easily understandable as much as I can. I have used the sort for the ribbon part though. There are many more things in the solution that can be skimmed down but for the sake of understandable code I decided to keep it this way.

Peace

1

u/PM_ME_INSIDER_INFO Dec 03 '15

Alright. It may just be your perspective but I get lost in the if statements. I do believe the sorting function would be more understandable by a larger audience.