-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday15.py
executable file
·58 lines (43 loc) · 856 Bytes
/
day15.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2023, 15)
DEBUG = 'debug' in map(str.lower, sys.argv)
fin = advent.get_input(mode='rb')
data = fin.read()
ans1 = ans2 = 0
def h(s):
n = 0
for c in s:
n += c
n *= 17
n %= 256
return n
for s in map(bytes.strip, data.split(b',')):
ans1 += h(s)
advent.print_answer(1, ans1)
def bdel(a, lst: list):
for i, (x, v) in enumerate(lst):
if a == x:
break
else:
return len(lst)
lst.pop(i)
return i
box = defaultdict(list)
def hm(s):
if b'-' in s:
a = s[:-1]
k = h(a)
bdel(a, box[k])
if b'=' in s:
a, b = s.split(b'=')
b = int(b)
k = h(a)
i = bdel(a, box[k])
box[k].insert(i, (a, b))
for s in map(bytes.strip, data.split(b',')):
hm(s)
for i, lst in box.items():
for j, (a, v) in enumerate(lst, 1):
ans2 += (i + 1) * j * v
advent.print_answer(2, ans2)