Skip to content

Commit

Permalink
test: day03
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 23, 2023
1 parent 79fb9cc commit f5f01c8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
4 changes: 4 additions & 0 deletions day03/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
33 changes: 18 additions & 15 deletions day03/day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
10 changes: 10 additions & 0 deletions day03/input-small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
Empty file added day03/tests/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions day03/tests/test_classes.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f5f01c8

Please sign in to comment.