43 lines
902 B
Python
43 lines
902 B
Python
import functools
|
|
|
|
def parse(file):
|
|
rules = []
|
|
for line in file:
|
|
line = line.strip()
|
|
if line == "":
|
|
break
|
|
a, _, b = line.partition('|')
|
|
rules.append((a,b))
|
|
updates = []
|
|
for line in file:
|
|
line = line.strip()
|
|
updates.append(line.split(','))
|
|
return rules, updates
|
|
|
|
def solve(file):
|
|
rules, updates = parse(file)
|
|
rules = set(rules)
|
|
def isvalid(pages):
|
|
for i in range(len(pages)):
|
|
p = pages[i]
|
|
for g in pages[i+1:]:
|
|
if (g,p) in rules:
|
|
return False
|
|
return True
|
|
|
|
t = 0
|
|
invalid = []
|
|
for pages in updates:
|
|
if isvalid(pages):
|
|
print(pages)
|
|
t += int(pages[len(pages)//2])
|
|
else:
|
|
invalid.append(pages)
|
|
print(t)
|
|
|
|
|
|
|
|
|
|
solve(open("sample1.in"))
|
|
solve(open("input"))
|