49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
|
import sys
|
||
|
|
||
|
def main():
|
||
|
lines = sys.stdin.read().splitlines()
|
||
|
|
||
|
order_rules = set()
|
||
|
updates = []
|
||
|
i = 0
|
||
|
n = len(lines)
|
||
|
while i < n and lines[i].strip():
|
||
|
line = lines[i].strip()
|
||
|
if '|' in line:
|
||
|
x, y = line.split('|')
|
||
|
x = int(x)
|
||
|
y = int(y)
|
||
|
order_rules.add((x, y))
|
||
|
i += 1
|
||
|
|
||
|
while i < n and not lines[i].strip():
|
||
|
i += 1
|
||
|
|
||
|
while i < n:
|
||
|
line = lines[i].strip()
|
||
|
if line:
|
||
|
update = [int(x) for x in line.split(',')]
|
||
|
updates.append(update)
|
||
|
i += 1
|
||
|
|
||
|
middle_pages = []
|
||
|
|
||
|
for update in updates:
|
||
|
position = {page: idx for idx, page in enumerate(update)}
|
||
|
valid = True
|
||
|
for x, y in order_rules:
|
||
|
if x in position and y in position:
|
||
|
if position[x] >= position[y]:
|
||
|
valid = False
|
||
|
break
|
||
|
|
||
|
if valid:
|
||
|
mid_index = len(update) // 2
|
||
|
middle_page = update[mid_index]
|
||
|
middle_pages.append(middle_page)
|
||
|
|
||
|
total = sum(middle_pages)
|
||
|
print(total)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|