-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
36 lines (27 loc) · 780 Bytes
/
main.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
from collections import defaultdict
with open("input.txt") as f:
constraints, updates = [x.split() for x in f.read().split("\n\n")]
d = defaultdict(list)
for c in constraints:
a, b = c.split("|")
d[a].append(b)
part1 = 0
part2 = 0
def repair_update(ud):
new_ud = []
for u in ud:
counter = len(new_ud)
while u in d and any(k in new_ud[:counter] for k in d[u]):
counter -= 1
new_ud.insert(counter, u)
return int(new_ud[len(new_ud) // 2])
for update in updates:
ud = update.split(",")
for i, u in enumerate(ud):
if u in d and any(k in ud[:i] for k in d[u]):
part2 += repair_update(ud)
break
else:
part1 += int(ud[len(ud) // 2])
print(part1)
print(part2)