-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (46 loc) · 1.27 KB
/
main.py
File metadata and controls
59 lines (46 loc) · 1.27 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from collections import defaultdict
ll = open("../inputs/day15.txt").read().strip().split(",")
p1 = 0
p2 = 0
def hash(l):
v = 0
for ch in l:
v += ord(ch)
v *= 17
v %= 256
return v
lenses = [[] for _ in range(256)]
lenslengths = [{} for _ in range(256)]
for i, l in enumerate(ll):
p1 += hash(l)
label = l.split("=")[0].split("-")[0]
v = hash(label)
if "-" in l:
if label in lenses[v]:
lenses[v].remove(label)
if "=" in l:
if label not in lenses[v]:
lenses[v].append(label)
lenslengths[v][label] = int(l.split("=")[1])
for box, lns in enumerate(lenses):
for i, lens in enumerate(lns):
p2 += (box + 1) * (i + 1) * lenslengths[box][lens]
print(p1, p2)
def hash_(str):
h = 0
for c in str:
h += ord(c)
h = (17 * h) % 256
return h
if __name__ == "__main__":
data = open("../inputs/day15.txt").read().strip().split(",")
print("part1: ", sum(hash_(d) for d in data))
boxes = defaultdict(dict)
for cmd in data:
if "-" in cmd:
label = cmd[:-1]
h = hash_(label)
boxes[hash_(label)].pop(label, None)
else:
label, i = cmd.split("=")
boxes[hash_(label)][label] = int(i)