From d52a3a1e5968dc38168d892fc27c394f6d30defa Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 3 Dec 2023 15:16:56 +0000 Subject: [PATCH] 2023: d03: ex1: add solution --- 2023/d03/ex1/ex1.py | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 2023/d03/ex1/ex1.py diff --git a/2023/d03/ex1/ex1.py b/2023/d03/ex1/ex1.py new file mode 100755 index 0000000..36cb548 --- /dev/null +++ b/2023/d03/ex1/ex1.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python + +import sys +from collections.abc import Iterator +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + + +class SchematicNumber(NamedTuple): + value: int + start: Point + + def neighbours(self) -> Iterator["Point"]: + # How long is the number + length = len(str(self.value)) + + # Every point to the left + for dx in range(-1, 1 + 1): + yield Point(self.start.x + dx, self.start.y - 1) + + # Every point below/above + for dy in range(length): + for dx in (-1, 1): + yield Point(self.start.x + dx, self.start.y + dy) + + # Every point to the right + for dx in range(-1, 1 + 1): + yield Point(self.start.x + dx, self.start.y + length) + + +SymbolsMap = dict[Point, str] + + +def parse_line(x: int, line: str) -> tuple[set[SchematicNumber], SymbolsMap]: + numbers: set[SchematicNumber] = set() + symbols: SymbolsMap = {} + + y = 0 + while y != len(line): + char = line[y] + pos = Point(x, y) + if char.isdigit(): + dy = 0 + while (y + dy) < len(line) and (line[y + dy]).isdigit(): + dy += 1 + numbers.add(SchematicNumber(value=int(line[y : y + dy]), start=pos)) + y += dy + continue + elif char == ".": + pass + else: + symbols[pos] = char + y += 1 + + return numbers, symbols + + +def parse(input: list[str]) -> tuple[set[SchematicNumber], SymbolsMap]: + numbers: set[SchematicNumber] = set() + symbols: SymbolsMap = {} + + for x, line in enumerate(input): + new_numbers, new_symbols = parse_line(x, line) + numbers |= new_numbers + symbols |= new_symbols + + return numbers, symbols + + +def solve(input: list[str]) -> int: + numbers, symbols = parse(input) + part_numbers: set[SchematicNumber] = set() + + for n in numbers: + if any(p in symbols for p in n.neighbours()): + part_numbers.add(n) + + return sum(n.value for n in part_numbers) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()