2023 day 15.
This commit is contained in:
parent
d0970c090b
commit
ba5b01c594
@ -1,13 +1,31 @@
|
|||||||
import sys
|
import sys
|
||||||
from collections import defaultdict
|
from functools import reduce
|
||||||
from dataclasses import dataclass
|
|
||||||
|
steps = sys.stdin.read().strip().split(",")
|
||||||
|
|
||||||
|
|
||||||
|
def _hash(s: str) -> int:
|
||||||
|
return reduce(lambda v, u: ((v + ord(u)) * 17) % 256, s, 0)
|
||||||
|
|
||||||
lines = sys.stdin.read().splitlines()
|
|
||||||
|
|
||||||
# part 1
|
# part 1
|
||||||
answer_1 = ...
|
answer_1 = sum(map(_hash, steps))
|
||||||
print(f"answer 1 is {answer_1}")
|
print(f"answer 1 is {answer_1}")
|
||||||
|
|
||||||
# part 2
|
# part 2
|
||||||
answer_2 = ...
|
boxes: list[dict[str, int]] = [{} for _ in range(256)]
|
||||||
|
|
||||||
|
for step in steps:
|
||||||
|
if (i := step.find("=")) >= 0:
|
||||||
|
label, length = step[:i], int(step[i + 1 :])
|
||||||
|
boxes[_hash(label)][label] = length
|
||||||
|
else:
|
||||||
|
label = step[:-1]
|
||||||
|
boxes[_hash(label)].pop(label, None)
|
||||||
|
|
||||||
|
answer_2 = sum(
|
||||||
|
i_box * i_lens * length
|
||||||
|
for i_box, box in enumerate(boxes, start=1)
|
||||||
|
for i_lens, length in enumerate(box.values(), start=1)
|
||||||
|
)
|
||||||
print(f"answer 2 is {answer_2}")
|
print(f"answer 2 is {answer_2}")
|
||||||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1 @@
|
|||||||
|
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
Loading…
Reference in New Issue
Block a user