-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.py
65 lines (50 loc) · 1.44 KB
/
15.py
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
60
61
62
63
64
65
data = open('15.txt').read().rstrip()
def HASH(step):
value = 0
for c in step:
value += ord(c)
value *= 17
value %= 256
return value
assert sum(HASH(step) for step in data.split(',')) == 495972
class Node:
def __init__(self, val=None, prev=None, next_=None):
self.val = val
self.prev = prev
self.next = next_
class HASHMAP:
def __init__(self):
self.start = self.last = Node()
self.hash = {}
def __setitem__(self, label, val):
if label in self.hash:
self.hash[label].val = val
return
n = Node(val, self.last)
self.last.next = n
self.last = n
self.hash[label] = n
def __delitem__(self, label):
if label not in self.hash:
return
n = self.hash[label]
self.hash.pop(label)
if n == self.last:
self.last = n.prev
n.prev.next = None
return
n.prev.next, n.next.prev = n.next, n.prev
def __iter__(self):
n = self.start.next
while n != None:
yield n.val
n = n.next
boxes = [HASHMAP() for _ in range(256)]
for d in data.split(','):
label, f = d.replace('-', '=').split('=')
h = HASH(label)
if not f:
del boxes[h][label]
continue
boxes[h][label] = f
assert sum(sum(i*j*int(f) for j, f in enumerate(box, 1)) for i, box in enumerate(boxes, 1)) == 245223