Advent Of Code 2015 - Day 02

Advent of Code 2015 - Day 02: I Was Told There Would Be No Math

Day 02 of the 2015 AoC is again a fairly easy problem.

Part 1

1
2
3
4
5
6
7
8
9
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.

For example:

A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet.
A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet.
All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order?

The input to our problem contains the dimensions of packages, with each present box printed on a seperate line:

1
2
3
4
5
[...]
20x3x11
15x27x5
6x29x7
[...]

After reading the input, we have to extract the three dimensions (w,h, and l) and convert these numbers to ints. For the extra paper, we need two find the two smallest number out of these three, so in a final preprocessing step the dimensions are sorted.

1
2
3
4
5
6
7
# read inputFile
with open(inputFile) as f:
        lines = f.read().splitlines()

input_un = [list(map(int, l.split('x'))) for l in lines]
# sort numbers
input = [sorted(l) for l in input_un]

The data now looks like this:

1
[[3,11,20],[5,15,27],[6,7,29]]

For the actual computation, my first idea to pythonic approach was to apply a lambda function to each entry, but list unpacking in lambda function does not work the way I expected; so I applied a named function with the use of the ‘*'-operator to the input.

1
2
3
4
# function to compute part1
def f1(a, b, c): return 2*(a*b+a*c+b*c)+a*b
p1 = [f1(*i) for i in input]
part1 = sum(p1)

Since the dimensions are sorted, the computation of smallest area is simply the product of the first two numbers.

Part 2

1
2
3
4
5
6
7
8
9
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.

For example:

A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of ribbon to wrap the present plus 2*3*4 = 24 feet of ribbon for the bow, for a total of 34 feet.
A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of ribbon to wrap the present plus 1*1*10 = 10 feet of ribbon for the bow, for a total of 14 feet.
How many total feet of ribbon should they order?

Taking advantage of the sorting of the input for part 1, the second part is now very easy to solve:

1
2
3
4
# function to compute part2
def f2(a, b, c): return 2*(a+b)+a*b*c
p2 = [f2(*i) for i in input]
part2 = sum(p2)