58 lines
1.1 KiB
Python
58 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
input_file = sys.argv[1]
|
|
|
|
with open(input_file) as fd:
|
|
lines = [line.rstrip() for line in fd.readlines()]
|
|
|
|
page_mode = False
|
|
orders: list[tuple[int, int]] = list()
|
|
|
|
class Page:
|
|
def __init__(self, pn: int):
|
|
self.pn = pn
|
|
|
|
def __lt__(self, other: "Page") -> bool:
|
|
a = self.pn
|
|
b = other.pn
|
|
for fi, se in orders:
|
|
if a == fi and b == se:
|
|
return True
|
|
elif a == se and b == fi:
|
|
return False
|
|
raise RuntimeError
|
|
|
|
|
|
updates: list[list[Page]] = list()
|
|
|
|
for line in lines:
|
|
if not page_mode:
|
|
if line == "":
|
|
page_mode = True
|
|
else:
|
|
order = tuple(int(a) for a in line.split("|"))
|
|
assert len(order) == 2
|
|
orders.append(order)
|
|
else:
|
|
update = list(Page(int(a)) for a in line.split(","))
|
|
updates.append(update)
|
|
|
|
total = 0
|
|
for update in updates:
|
|
update_sorted = sorted(update)
|
|
|
|
if update == update_sorted:
|
|
continue
|
|
|
|
update = update_sorted
|
|
|
|
# Add
|
|
imid = int(len(update)/2)
|
|
mid = update[imid]
|
|
total += mid.pn
|
|
|
|
print(total)
|
|
|