AoC 2015 Puzzle 2

2015 Puzzle 2: I Was Told There Would Be No Math

Puzzle, part 1

The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need.

Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side.

All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?

Solution, part 1

The first thing to do is to process each entry in the input file. Each line is a set of dimensions. The input is split on a x which yields the 3 parts of the dimension.

For each line the calculation function is called.

private int processEntry(List<String> input, int part) {
    int order = 0;

    for (String present : input) {
        String[] parts = present.split("x");
        order += calculateWrapping(parts);
    }

    return order;
}

The split of the dimensions yields an array of String objects. Those need to be converted to int in order to do any calculations. For this the Arrays.stream() function can be used, this creates a Java 8 stream of any array.

Next the 3 dimensions can be calculated. The calculation is a little bit cumbersome as we need to know the smallest side. This can be achieved by using Arrays.sort(). This will order the values in the dim array from small to large,

Lastly another Arrays.stream() trick is used to iterate the dim array, multiply each entry with 2 and then summing the result. Additionally the smallest dimension is added to the result.

private int calculateWrapping(String[] parts) {
    int[] p = Arrays.stream(parts).mapToInt(e -> Integer.valueOf(e)).toArray();

    int[] dim = new int[3];
    // 2*l*w + 2*w*h + 2*h*l
    dim[0] = p[0] * p[1];
    dim[1] = p[1] * p[2];
    dim[2] = p[2] * p[0];

    Arrays.sort(dim);

    return Arrays.stream(dim).map(entry -> 2 * entry).sum() + dim[0];
}

Puzzle, part 2

The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact.

The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don't ask how they tie the bow, though; they'll never tell.

How many total feet of ribbon should they order?

Solution, part 2

The ribbons are a variation of the calculation. The same input is needed, so processEntries can be modified with a simple if statement to separate the parts.

if (part == 1)
    order += calculateWrapping(parts);
else if (part == 2)
    order += calculateRibbon(parts);

The ribbon calculation uses the same Arrays functions to convert and sort the arrays. The calculation required is straightforward. The only thing to remember is that we should take the 2 smallest sides, which we achieved using the Arrays.sort().

private int calculateRibbon(String[] parts) {
    int[] p = Arrays.stream(parts).mapToInt(e -> Integer.valueOf(e)).toArray();

    Arrays.sort(p);

    return 2 * p[0] + 2 * p[1] + p[0] * p[1] * p[2];
}

Conclusion

This puzzle introduced several Arrays functions that can be used to make working with arrays a lot more modern.

Lets see what Puzzle 3 brings.