From 5c352109de6ba5cf9a2305363974c74e3a3c15d6 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 8 Dec 2024 05:16:44 +0000 Subject: [PATCH] 2024: d08: ex1: add solution --- 2024/d08/ex1/ex1.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 2024/d08/ex1/ex1.py diff --git a/2024/d08/ex1/ex1.py b/2024/d08/ex1/ex1.py new file mode 100755 index 0000000..14170b4 --- /dev/null +++ b/2024/d08/ex1/ex1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +import itertools +import sys +from collections import defaultdict +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + + +def solve(input: str) -> int: + def parse(input: list[str]) -> dict[str, set[Point]]: + res: dict[str, set[Point]] = defaultdict(set) + for x, line in enumerate(input): + for y, c in enumerate(line): + if c == ".": + continue + res[c].add(Point(x, y)) + return res + + def find_antinodes(grid: dict[str, set[Point]], dims: Point) -> set[Point]: + max_x, max_y = dims + antinodes: set[Point] = set() + for antennas in grid.values(): + for start, end in itertools.permutations(antennas, 2): + dx, dy = end.x - start.x, end.y - start.y + x, y = start.x - dx, start.y - dy + if not (0 <= x < max_x): + continue + if not (0 <= y < max_y): + continue + antinodes.add(Point(x, y)) + return antinodes + + lines = input.splitlines() + max_x, max_y = len(lines), len(lines[0]) + grid = parse(lines) + return len(find_antinodes(grid, Point(max_x, max_y))) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()