summaryrefslogtreecommitdiff
path: root/02/main.py
blob: 0a1697de746c6640132dee1c285544c69ced43cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/python



def solve_part1(boxes):
    result = 0
    for b in boxes:
        faces = [b[0]* b[1], b[1] * b[2], b[2] * b[0]]
        result += 2 * sum(faces) + min(faces)
    return result



def solve_part2(boxes):
    result = 0
    for b in boxes:
        perims = [2 * (b[0] + b[1]), 2 * (b[1] + b[2]), 2 * (b[2] + b[0])]
        volume = b[0] * b[1] * b[2]
        result += min(perims) + volume
    return result



def main():
    # Read box dimensions from input text file
    with open("input.txt", "r") as f:
        lines = f.read().splitlines()

    # Parse the dimensions into tuples (x, y, z)
    boxes = []
    for line in lines:
        dims = line.split("x")
        b = tuple([int(d) for d in dims])
        boxes.append(b)

    print("Part 1 solution:", solve_part1(boxes)) # 1586300 for me
    print("Part 2 solution:", solve_part2(boxes)) # 3737498 for me



if __name__ == "__main__":
    main()