Part 1: A gift requires enough wrapping paper to cover the surface plus an additional amount equal to the area smallest side. Calculate the total wrapping paper needed for a list of dimensions of the form 2x3x4
.
total_area = 0
for line in sys.stdin:
l, w, h = list(sorted(map(int, line.strip().split('x'))))
area = 3 * l * w + 2 * w * h + 2 * h * l
total_area += area
print(total_area)
The only real trick here is the use of list(sorted(...))
. This will guarantee that l
and w
are the smallest dimensions and thus represent the extra area to add.
Part 2: Given the same input, calculate the amount of ribbon needed. You need the larger of either the shortest distance around the outside or the smallest perimeter of any one face. In addition, you need an additional amount equal to the volume in cubic feet.
total_ribbon = 0
for line in sys.stdin:
l, w, h = list(sorted(map(int, line.strip().split('x'))))
total_ribbon += max(
2 * (l + w), # smallest distance around sides
4 * l, # smallest perimeter
)
total_ribbon += l * w * h
print(total_ribbon)
This one was a little stranger since the original description was unclear if you needed the larger or smaller of the first two measurements, but it was easy enough to calculate both. Turns out, they meant the larger of those two.