August 13, 2023

Solution for Advent of Code 2015 - Day 2: I Was Told There Would Be No Math

For each paket, we need to find the surface area of the box. After parsing the input into the three integers for width w, height h and length l, the surface area for a box is 2 * (w * h + w * l + l * h). For the puzzle we also need add the smallest side of box again. We do this by adding min(w * h, w * l, l * h).
 
def wrapping_area(dimensions_line):
l, w, h = dimensions_line.split("x")
l = int(l)
w = int(w)
h = int(h)

area = 2 * l * w + 2 * w * h + 2 * l * h + min(l * w, w * h, l * h)
return area

This function passed the tests against the supplied inputs and for my real puzzle, we just loop over all lines in the input.

Part 2

In part 2 we need to calculate the length of a ribbon for the package. The length is made of two parts: the perimeter of the smallest side and a bow. The perimeter of the smallest side is 2 * (smallest dimension * second smallest dimension) or since we have only three dimensions: 2 * (sum of all dimension - largest dimension). The bow is simply the volume of the package length * width * height.
 
def ribbon_length(dimensions_line):
l, w, h = dimensions_line.split("x")
l = int(l)
w = int(w)
h = int(h)

ribbon = 2 * (l + w + h - max(l, w, h))
ribbon += l * w * h
return ribbon

This was again tested against the supplied inputs and calcaluted in a loop for the real puzzle.

Link to my solutions

No comments:

Post a Comment