diff options
-rw-r--r-- | 15.py | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -0,0 +1,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) |