From e688c2b674fc7ad6a964a48df379e5abd01843a7 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Mon, 4 Dec 2023 10:40:34 +0100 Subject: Day4 --- day3/__init__.py | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'day3') diff --git a/day3/__init__.py b/day3/__init__.py index ebe243e..aca90da 100644 --- a/day3/__init__.py +++ b/day3/__init__.py @@ -8,7 +8,9 @@ from aoc.datastructures import Coordinate class Assignment(BaseAssignment, ABC): - def symbol_overlap(self, number_coordinates: list[Coordinate], symbols: set[Coordinate]) -> set[Coordinate]: + def symbol_overlap( + self, number_coordinates: list[Coordinate], symbols: set[Coordinate] + ) -> set[Coordinate]: neighbours = { neighbour for coordinate in number_coordinates @@ -17,7 +19,9 @@ class Assignment(BaseAssignment, ABC): return neighbours & symbols - def read_input(self, example=False) -> tuple[list[str], list[tuple[list[Coordinate], int]], set[Coordinate]]: + def read_input( + self, example=False + ) -> tuple[list[str], list[tuple[list[Coordinate], int]], set[Coordinate]]: schematic = list(super().read_input(example)) numbers: list[tuple[list[Coordinate], int]] = list() symbols: set[Coordinate] = set() @@ -28,7 +32,7 @@ class Assignment(BaseAssignment, ABC): for x, item in enumerate(row): if item.isdigit(): if number is None: - number = [[], ''] + number = [[], ""] number[0].append(Coordinate(x, y)) number[1] += item @@ -38,7 +42,7 @@ class Assignment(BaseAssignment, ABC): numbers.append((number[0], int(number[1]))) number = None - if item != '.': + if item != ".": symbols.add(Coordinate(x, y)) if number is not None: @@ -50,10 +54,14 @@ class Assignment(BaseAssignment, ABC): class AssignmentOne(Assignment): example_result = 4361 - def is_part_number(self, number_coordinates: list[Coordinate], symbols: set[Coordinate]) -> bool: + def is_part_number( + self, number_coordinates: list[Coordinate], symbols: set[Coordinate] + ) -> bool: return len(self.symbol_overlap(number_coordinates, symbols)) > 0 - def run(self, input: tuple[list[tuple[list[Coordinate], int]], set[Coordinate]]) -> int: + def run( + self, input: tuple[list[tuple[list[Coordinate], int]], set[Coordinate]] + ) -> int: _, numbers, symbols = input part_numbers = [] @@ -68,14 +76,16 @@ class AssignmentOne(Assignment): class AssignmentTwo(Assignment): example_result = 467835 - def run(self, input: tuple[list[tuple[list[Coordinate], int]], set[Coordinate]]) -> int: + def run( + self, input: tuple[list[tuple[list[Coordinate], int]], set[Coordinate]] + ) -> int: schematic, numbers, symbols = input possible_gears = {} for coordinates, number in numbers: for symbol in self.symbol_overlap(coordinates, symbols): - if schematic[symbol.y][symbol.x] != '*': + if schematic[symbol.y][symbol.x] != "*": continue if symbol not in possible_gears: @@ -83,12 +93,10 @@ class AssignmentTwo(Assignment): possible_gears[symbol].append(number) - return sum([ - reduce( - lambda total, number: total * number, - numbers, - 1 - ) - for symbol, numbers in possible_gears.items() - if len(numbers) > 1 - ]) + return sum( + [ + reduce(lambda total, number: total * number, numbers, 1) + for symbol, numbers in possible_gears.items() + if len(numbers) > 1 + ] + ) -- cgit v1.2.3