Compare commits
No commits in common. "2fb5ac7e8fb5ef283021ba1e686b75b8e5e6853c" and "0df3ad93e87f6344f2c8e388150c236abca02f88" have entirely different histories.
2fb5ac7e8f
...
0df3ad93e8
@ -1,48 +0,0 @@
|
|||||||
def parse(filename: str) -> tuple[list[tuple[int, int]], list[tuple[int, ...]]]:
|
|
||||||
page_orders = []
|
|
||||||
updates = []
|
|
||||||
with open(filename) as f:
|
|
||||||
first_part = True
|
|
||||||
while line := f.readline():
|
|
||||||
if first_part and line != "\n":
|
|
||||||
page_orders.append(tuple(map(int, line.strip().split("|"))))
|
|
||||||
else:
|
|
||||||
first_part = False
|
|
||||||
if not first_part and line != "\n":
|
|
||||||
updates.append(tuple(map(int, filter(lambda x: x != "", line.strip().split(",")))))
|
|
||||||
return page_orders, list(map(lambda u: tuple(map(int, u)), updates))
|
|
||||||
|
|
||||||
|
|
||||||
def get_middle_page(update: tuple[int, ...]) -> int:
|
|
||||||
return update[int(len(update) / 2)]
|
|
||||||
|
|
||||||
|
|
||||||
def update_is_correct(
|
|
||||||
update: tuple[int, ...], not_before: dict[int, list[int]], not_after: dict[int, list[int]]
|
|
||||||
) -> bool:
|
|
||||||
correct = True
|
|
||||||
for index, page in enumerate(update[:-1]):
|
|
||||||
correct = all(
|
|
||||||
map(
|
|
||||||
lambda x: x in not_before.get(page, []) and x not in not_after.get(page, []),
|
|
||||||
update[index + 1 :],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if not correct:
|
|
||||||
break
|
|
||||||
return correct
|
|
||||||
|
|
||||||
|
|
||||||
def condense_page_orders(page_orders: list[tuple[int, int]]) -> tuple[dict[int, list[int]], dict[int, list[int]]]:
|
|
||||||
not_before = {}
|
|
||||||
not_after = {}
|
|
||||||
for item in page_orders:
|
|
||||||
if item[0] in not_before:
|
|
||||||
not_before[item[0]].append(item[1])
|
|
||||||
else:
|
|
||||||
not_before[item[0]] = [item[1]]
|
|
||||||
if item[1] in not_after:
|
|
||||||
not_after[item[1]].append(item[0])
|
|
||||||
else:
|
|
||||||
not_after[item[1]] = [item[0]]
|
|
||||||
return not_before, not_after
|
|
@ -1,18 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
from common import condense_page_orders, get_middle_page, parse, update_is_correct
|
|
||||||
|
|
||||||
|
|
||||||
def solve(input: tuple[list[tuple[int, int]], list[tuple[int, ...]]]) -> int:
|
|
||||||
raw_page_orders, updates = input
|
|
||||||
not_before, not_after = condense_page_orders(raw_page_orders)
|
|
||||||
middle_pages = map(get_middle_page, filter(lambda u: update_is_correct(u, not_before, not_after), updates))
|
|
||||||
|
|
||||||
return sum(middle_pages)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(solve(parse("input")))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -1,23 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
from common import condense_page_orders, get_middle_page, parse, update_is_correct
|
|
||||||
|
|
||||||
|
|
||||||
def reorder(incorrect_update: tuple[int, ...], not_before, not_after) -> tuple[int, ...]:
|
|
||||||
return tuple()
|
|
||||||
|
|
||||||
|
|
||||||
def solve(input: tuple[list[tuple[int, int]], list[tuple[int, ...]]]) -> int:
|
|
||||||
raw_page_orders, updates = input
|
|
||||||
not_before, not_after = condense_page_orders(raw_page_orders)
|
|
||||||
incorrect_updates = filter(lambda u: not update_is_correct(u, not_before, not_after), updates)
|
|
||||||
middle_pages = [map(get_middle_page, sorted(incorrect_updates, key=lambda x: reorder(x, not_before, not_after)))]
|
|
||||||
|
|
||||||
return sum(middle_pages)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
print(solve(parse("input")))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
Loading…
x
Reference in New Issue
Block a user