diff --git a/day03/classes.py b/day03/classes.py index f6d007f..2181abd 100644 --- a/day03/classes.py +++ b/day03/classes.py @@ -126,3 +126,7 @@ def find_gear_parts( if part_number.touching(gear.col, gear.row, self.row_size): result.append(part_number) return result + + def filter_engine_parts(self, part_numbers: list[PartNumber]) -> list[PartNumber]: + """Return the legit part numbers""" + return list(filter(self.is_engine_part, part_numbers)) diff --git a/day03/day3.py b/day03/day3.py index 9bde5d7..b92dcb6 100644 --- a/day03/day3.py +++ b/day03/day3.py @@ -2,37 +2,40 @@ Day 3 implementation """ -from typing import TYPE_CHECKING -from day03.classes import Gear, Matrix +from day03.classes import Gear, Matrix, PartNumber -if TYPE_CHECKING: - from day03.classes import PartNumber +INPUT = "day03/input.txt" +INPUT_SMALL = "day03/input-small.txt" -def get_data() -> Matrix: +def get_data(path: str) -> Matrix: """Convert text file to matrix""" - with open("day03/input.txt", "r", encoding="utf8") as file: + with open(path, "r", encoding="utf8") as file: data = file.readlines() data = [line.strip() for line in data] return Matrix(data=data) -def main() -> None: - matrix = get_data() - part_numbers = matrix.get_part_numbers() +def part1(part_numbers: list[PartNumber]) -> int: + return sum([part_number.value for part_number in part_numbers]) + + +def part2(part_numbers: list[PartNumber], matrix: Matrix) -> int: + gears: list[Gear] = matrix.get_gears(part_numbers) + return sum(gear.gear_ratio for gear in gears) - def part_filter(part_number: "PartNumber") -> bool: - return matrix.is_engine_part(part_number) - part_numbers = list(filter(part_filter, part_numbers)) +def main() -> None: + matrix: Matrix = get_data(INPUT) + part_numbers: list["PartNumber"] = matrix.get_part_numbers() + part_numbers = matrix.filter_engine_parts(part_numbers) # q1 - print(sum([part_number.value for part_number in part_numbers])) + print(part1(part_numbers)) # q2 - gears: list[Gear] = matrix.get_gears(part_numbers) - print(sum(gear.gear_ratio for gear in gears)) + print(part2(part_numbers, matrix)) if __name__ == "__main__": diff --git a/day03/input-small.txt b/day03/input-small.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/day03/input-small.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/day03/tests/__init__.py b/day03/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day03/tests/test_classes.py b/day03/tests/test_classes.py new file mode 100644 index 0000000..fc44553 --- /dev/null +++ b/day03/tests/test_classes.py @@ -0,0 +1,35 @@ +from dataclasses import dataclass + +from day03.classes import PartNumber + + +@dataclass +class PartNumberTouchTest: + col: int + row: int + row_size: int + result: bool + + +def test_part_number() -> None: + part_number = PartNumber(0, 0, 3, 467) + + assert part_number.end_index == 3 + + tests = [ + PartNumberTouchTest(0, 0, 10, True), + PartNumberTouchTest(0, 0, 10, True), + PartNumberTouchTest(1, 0, 10, True), + PartNumberTouchTest(2, 0, 10, True), + PartNumberTouchTest(3, 0, 10, True), + PartNumberTouchTest(0, 0, 10, True), + PartNumberTouchTest(1, 1, 10, True), + PartNumberTouchTest(2, 1, 10, True), + PartNumberTouchTest(3, 1, 10, True), + PartNumberTouchTest(1, -1, 10, True), + PartNumberTouchTest(2, -1, 10, True), + PartNumberTouchTest(3, -1, 10, True), + PartNumberTouchTest(4, 0, 10, False), + ] + for test in tests: + assert part_number.touching(test.col, test.row, test.row_size) == test.result