42 lines
906 B
Python
42 lines
906 B
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()
|
||
|
updates: list[list[int]] = 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(int(a) for a in line.split(","))
|
||
|
updates.append(update)
|
||
|
|
||
|
total = 0
|
||
|
for update in updates:
|
||
|
for fi, se in orders:
|
||
|
try:
|
||
|
ifi = update.index(fi)
|
||
|
ise = update.index(se)
|
||
|
except ValueError:
|
||
|
continue
|
||
|
if ifi > ise:
|
||
|
break
|
||
|
else:
|
||
|
imid = int(len(update)/2)
|
||
|
mid = update[imid]
|
||
|
total += mid
|
||
|
print(total)
|
||
|
|