main.py

#

Part 1:

— Day 2: I Was Told There Would Be No Math —

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.

from heapq import nsmallest
#
#

For example:

def test_part1() -> None:
#

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.

    assert part1("2x3x4") == 58
#

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.

    assert part1("1x1x10") == 43
#

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

#

Part 1 Solution:

#

First, the input lines have to be parsed into integer dimensions.

>>> parse_dimensions("2x3x4")
(2, 3, 4)
def parse_dimensions(line: str) -> tuple[int, int, int]:
#
    parts = line.split("x")
    assert len(parts) == 3, f"Got dimension line that isn't three numbers: {line}"

    l, w, h = map(int, parts)
    return (l, w, h)
#

Calculate a present’s surface area plus extra.

def paper_area(length: int, width: int, height: int) -> int:
#

Get the area of each unique side

    sides = [length * width, width * height, height * length]
#

Save the smallest side as the amount of extra paper needed

    extra = min(sides)
#

Add up the doubles of each unique side and include the extra

    return sum(2 * side for side in sides) + extra
#

Split the input into separate present dimension lines, calculate each of their wrapping paper areas, and then return the total.

def part1(input: str) -> int:
#

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.

    return sum(paper_area(*parse_dimensions(line)) for line in input.splitlines())
#
#

For example:

def test_part2() -> None:
#

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.

    assert part2("2x3x4") == 34
#

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.

    assert part2("1x1x10") == 14
#

How many total feet of ribbon should they order?

#

Part 2 Solution:

#

Calculate a present’s required ribbon length

def ribbon_length(length: int, width: int, height: int) -> int:
#
#

Find the two smallest sides for the wrapped ribbon length

    short_sides = nsmallest(2, [length, width, height])
    wrapped_length = sum(side * 2 for side in short_sides)
#

The length of ribbon needed for the bow is equal to the present’s volume

    bow_length = length * width * height

    return wrapped_length + bow_length
#

Split the input into separate present dimension lines, calculate each of their required ribbon lengths, and then return the total.

def part2(input: str) -> int:
#
    return sum(ribbon_length(*parse_dimensions(line)) for line in input.splitlines())


if __name__ == "__main__":
    puzzle_input = open("input.txt").read()
#

Print out part 1 solution

    print("Part 1:", part1(puzzle_input))
#

Print out part 2 solution

    print("Part 2:", part2(puzzle_input))