2024 day 5.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2024-12-05 07:25:55 +01:00
parent b89d29e880
commit d7621d09b5
3 changed files with 1441 additions and 3 deletions

View File

@ -1,10 +1,49 @@
import sys import sys
from collections import defaultdict
lines = sys.stdin.read().splitlines() import networkx as nx
answer_1 = ...
answer_2 = ... def in_correct_order(update: list[int], requirements: dict[int, set[int]]) -> bool:
for i_value, value in enumerate(update):
if any(value_2 in requirements[value] for value_2 in update[i_value:]):
return False
return True
def fix_order(update: list[int], requirements: dict[int, set[int]]) -> list[int]:
graph = nx.DiGraph()
graph.add_nodes_from(update)
graph.add_edges_from(
(v, k)
for k, vs in requirements.items()
for v in vs
if k in update and v in update
)
return list(nx.topological_sort(graph))
part1, part2 = sys.stdin.read().split("\n\n")
requirements: dict[int, set[int]] = defaultdict(set)
for line in part1.splitlines():
v1, v2 = line.split("|")
requirements[int(v2)].add(int(v1))
updates = [list(map(int, line.split(","))) for line in part2.splitlines()]
answer_1 = sum(
update[len(update) // 2]
for update in updates
if in_correct_order(update, requirements)
)
answer_2 = sum(
fix_order(update, requirements)[len(update) // 2]
for update in updates
if not in_correct_order(update, requirements)
)
print(f"answer 1 is {answer_1}") print(f"answer 1 is {answer_1}")
print(f"answer 2 is {answer_2}") print(f"answer 2 is {answer_2}")

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47