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)
|
@ -295,4 +295,4 @@ and focus. Of course, learning more algorithms and techniques helps.
|
|||||||
- Day 2: `00:05:34 686 0 00:08:21 531 0`
|
- Day 2: `00:05:34 686 0 00:08:21 531 0`
|
||||||
- Day 3: `00:01:48 165 0 00:02:40 56 45`
|
- Day 3: `00:01:48 165 0 00:02:40 56 45`
|
||||||
- Day 4: `00:07:47 1101 0 00:24:07 2410 0`
|
- Day 4: `00:07:47 1101 0 00:24:07 2410 0`
|
||||||
- Day 5:
|
- Day 5: `00:07:07 679 0 00:23:02 1998 0`
|
||||||
|
Loading…
Reference in New Issue
Block a user