Skip to content
advent of code 2015

day 02

i was told there would be no math

approach

each line is parsed into a (l, w, h) tuple. two small helpers, area and perimeter, work on a pair of sides, and both parts look at the three pairs of sides of every box.

part 1 adds the full surface area plus the smallest face. part 2 adds the smallest face perimeter plus the volume.

the trick

compute the three faces once and take min over them for the extra bit.

python
def part1(presents):
    s = 0
    for l, w, h in presents:
        a1 = area(l, w)
        a2 = area(w, h)
        a3 = area(h, l)
        s += (2 * a1) + (2 * a2) + (2 * a3)
        s += min(a1, a2, a3)
    return s