-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.py
62 lines (43 loc) · 1.25 KB
/
05.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
from aoc.utilities.fetch import get_input
from aoc.utilities.decorators import solution
from collections import defaultdict
from functools import cmp_to_key
def parse(data):
edges = defaultdict(set)
updates = []
for line in data.splitlines():
if "|" in line:
a, b = map(int, line.split("|"))
edges[a].add(b)
elif line:
updates.append(list(map(int, line.split(","))))
return edges, updates
@solution
def solve_first(edges, updates):
ans = sum(x for pages in updates if (x := check_valid(edges, pages)) != -1)
print(ans)
@solution
def solve_second(edges, updates):
def comparator(i, j):
if j in edges[i]:
return -1
return 1
ans = 0
for pages in updates:
if check_valid(edges, pages) != -1:
continue
key = cmp_to_key(comparator)
pages.sort(key=key)
ans += pages[len(pages) // 2]
print(ans)
def check_valid(edges, pages):
n = len(pages)
for i in range(n):
for j in range(i + 1, n):
if pages[i] in edges[pages[j]]:
return -1
return pages[n // 2]
data = get_input(5)
edges, updates = parse(data)
solve_first(edges, updates)
solve_second(edges, updates)