Solve 2024 day 5
This commit is contained in:
parent
24174b8cb9
commit
63166ddce8
37
2024/d5.py
Normal file
37
2024/d5.py
Normal file
@ -0,0 +1,37 @@
|
||||
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)
|
Loading…
Reference in New Issue
Block a user