summaryrefslogtreecommitdiffstats
path: root/15.py
blob: cad8d00ceabb1572450f43f0b8f6f772028c13c8 (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
from sys import stdin


def holiday_hash(s: str) -> int:
    value = 0
    for c in s:
        value += ord(c)
        value *= 17
        value %= 256
    return value


inp = list(stdin.read().rstrip("\n").split(","))

print(sum(map(holiday_hash, inp)))

boxes: list[dict[str, int]] = [dict() for _ in range(256)]
for i in inp:
    if i.endswith("-"):
        label = i[:-1]
        h = holiday_hash(label)
        if label in boxes[h]:
            del boxes[h][label]
    else:
        label, operand = i.split("=", maxsplit=2)
        boxes[holiday_hash(label)][label] = int(operand)

p2 = 0
for i, box in enumerate(boxes):
    for slot, (label, focal_length) in enumerate(box.items()):
        print(i, slot, label, focal_length)
        p2 += (i + 1) * (slot + 1) * focal_length
print(p2)