-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
40 lines (28 loc) · 825 Bytes
/
solver.py
File metadata and controls
40 lines (28 loc) · 825 Bytes
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
34
35
36
37
38
39
40
import collections
import functools
import inspect
import os
cwd = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
with open(os.path.join(cwd, "example.txt"), "r") as f:
steps = f.read().splitlines()[0].split(",")
def hash(label):
return functools.reduce(lambda acc, char: (acc + ord(char)) * 17 % 256, label, 0)
# part 1
print(sum(hash(step) for step in steps))
# part 2
boxes = collections.defaultdict(dict)
for step in steps:
if step.endswith("-"):
label = step[:-1]
boxes[hash(label)].pop(label, -1)
else:
label, focal = step.split("=")
boxes[hash(label)][label] = int(focal)
print(
sum(
sum(
(box + 1) * (slot + 1) * focal for slot, focal in enumerate(lenses.values())
)
for box, lenses in boxes.items()
)
)