diff options
| author | Thomas Voss <mail@thomasvoss.com> | 2025-12-08 21:50:26 +0100 |
|---|---|---|
| committer | Thomas Voss <mail@thomasvoss.com> | 2025-12-08 21:50:26 +0100 |
| commit | 4b8d68d76ed8a612aac76e591157486b52c79d0e (patch) | |
| tree | c738c6383df37d75412ac6cdb90d4ca3f09735da /2025/08 | |
| parent | 7e7554af619355bdabcc08e10212a8947d9bf287 (diff) | |
Diffstat (limited to '2025/08')
| -rw-r--r-- | 2025/08/.gitignore | 1 | ||||
| -rw-r--r-- | 2025/08/Makefile | 1 | ||||
| -rw-r--r-- | 2025/08/puzzles.py | 53 |
3 files changed, 55 insertions, 0 deletions
diff --git a/2025/08/.gitignore b/2025/08/.gitignore new file mode 100644 index 0000000..ffc46fe --- /dev/null +++ b/2025/08/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py diff --git a/2025/08/Makefile b/2025/08/Makefile new file mode 100644 index 0000000..0a4e980 --- /dev/null +++ b/2025/08/Makefile @@ -0,0 +1 @@ +include ../../Makefiles/py.mk diff --git a/2025/08/puzzles.py b/2025/08/puzzles.py new file mode 100644 index 0000000..086baca --- /dev/null +++ b/2025/08/puzzles.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +import functools +import heapq +import itertools +import math +import operator + + +type Point = tuple[int, int, int] +type Circuit = set[Point] + + +def main() -> None: + with open('input', 'r') as f: + boxes = [tuple(map(int, l.split(','))) for l in f.readlines()] + + circuits: list[Circuit] = [] + pairs = itertools.combinations(boxes, 2) + pairs = iter(sorted(pairs, key=lambda p: math.dist(*p))) + +# START PART 1 + for p, q in itertools.islice(pairs, 1000): + connect(circuits, p, q) + + lens = heapq.nlargest(3, map(len, circuits)) + print(functools.reduce(operator.mul, lens)) +# END PART 1 START PART 2 + while len(circuits) != 1 or len(circuits[0]) != len(boxes): + p, q = next(pairs) + connect(circuits, p, q) + print(p[0] * q[0]) +# END PART 2 + + +def connect(circuits: list[Circuit], p: Point, q: Point) -> None: + pc = next((c for c in circuits if p in c), None) + qc = next((c for c in circuits if q in c), None) + + match (pc, qc): + case (None, None): + circuits.append({p, q}) + case (x, None): + pc.add(q) + case (None, y): + qc.add(p) + case (x, y) if x is not y: + circuits.remove(qc) + pc |= qc + + +if __name__ == '__main__': + main() |