diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-12-23 23:19:32 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-12-23 23:19:32 +0100 |
commit | 1153a91fd4b141ee9d5310e613459fa3ac770179 (patch) | |
tree | 2e91747754241f2d4addb3cefc3ba094cfd51db6 | |
parent | 455d7a3e6004c66ae372bbdd5362b24a56c46b63 (diff) | |
download | aoc2023-1153a91fd4b141ee9d5310e613459fa3ac770179.tar.gz aoc2023-1153a91fd4b141ee9d5310e613459fa3ac770179.tar.xz aoc2023-1153a91fd4b141ee9d5310e613459fa3ac770179.zip |
day 15
-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) |