summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-12-23 23:19:32 +0100
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-12-23 23:19:32 +0100
commit1153a91fd4b141ee9d5310e613459fa3ac770179 (patch)
tree2e91747754241f2d4addb3cefc3ba094cfd51db6
parent455d7a3e6004c66ae372bbdd5362b24a56c46b63 (diff)
downloadaoc2023-1153a91fd4b141ee9d5310e613459fa3ac770179.tar.gz
aoc2023-1153a91fd4b141ee9d5310e613459fa3ac770179.tar.xz
aoc2023-1153a91fd4b141ee9d5310e613459fa3ac770179.zip
day 15
-rw-r--r--15.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/15.py b/15.py
new file mode 100644
index 0000000..cad8d00
--- /dev/null
+++ b/15.py
@@ -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)