38 lines
845 B
Python
38 lines
845 B
Python
from lib import get_data
|
|
from lib import ints
|
|
|
|
|
|
def fix(xs):
|
|
change = True
|
|
while change:
|
|
change = False
|
|
for rule in rules.splitlines():
|
|
x, y = ints(rule)
|
|
if not (x in xs and y in xs):
|
|
continue
|
|
i, j = xs.index(x), xs.index(y)
|
|
if not i < j:
|
|
xs[i], xs[j] = xs[j], xs[i]
|
|
change = True
|
|
return xs
|
|
|
|
|
|
rules, pages = get_data(__file__).split("\n\n")
|
|
|
|
t1, t2 = 0, 0
|
|
for page in pages.splitlines():
|
|
xs = ints(page)
|
|
for rule in rules.splitlines():
|
|
x, y = ints(rule)
|
|
if not (x in xs and y in xs):
|
|
continue
|
|
if not (xs.index(x) < xs.index(y)):
|
|
xs = fix(ints(page))
|
|
t2 += xs[len(xs) // 2]
|
|
break
|
|
else:
|
|
t1 += xs[len(xs) // 2]
|
|
|
|
print(t1)
|
|
print(t2)
|