From fde618ecf68750e719733c2b46ae81f872d59f34 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 5 Dec 2024 10:47:08 +0000 Subject: [PATCH] 2024: d05: ex1: add solution --- 2024/d05/ex1/ex1.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 2024/d05/ex1/ex1.py diff --git a/2024/d05/ex1/ex1.py b/2024/d05/ex1/ex1.py new file mode 100755 index 0000000..90ed324 --- /dev/null +++ b/2024/d05/ex1/ex1.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +import collections +import sys + +Graph = dict[int, set[int]] + + +def solve(input: str) -> int: + def parse_ordering(input: str) -> Graph: + graph: Graph = collections.defaultdict(set) + for l in input.splitlines(): + lhs, rhs = map(int, l.split("|")) + graph[lhs].add(rhs) + return graph + + def parse_updates(input: str) -> list[list[int]]: + return [[int(n) for n in line.split(",")] for line in input.splitlines()] + + def parse(input: str) -> tuple[Graph, list[list[int]]]: + ordering, updates = input.split("\n\n") + return parse_ordering(ordering), parse_updates(updates) + + def validate_update(ordering: Graph, update: list[int]) -> bool: + for i in range(len(update)): + for j in range(i + 1, len(update)): + lhs, rhs = update[i], update[j] + if lhs in ordering[rhs]: + return False + return True + + def get_middle(update: list[int]) -> int: + assert len(update) % 2 == 1 # Sanity check + return update[len(update) // 2] + + ordering, updates = parse(input) + return sum( + get_middle(update) for update in updates if validate_update(ordering, update) + ) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()