From 80a0810f7682fdfa87580b10d19583cb44a2183f Mon Sep 17 00:00:00 2001 From: alexo Date: Thu, 28 Dec 2023 00:21:54 +1100 Subject: [PATCH] day16: coverage --- .pre-commit-config.yaml | 8 ++++++++ day16/lib/cells.py | 33 ++++++++++++++++++--------------- day16/lib/direction.py | 2 +- day16/tests/test_world.py | 31 +++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 5 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 day16/tests/test_world.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4bf7c1b..587204d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -50,4 +50,12 @@ repos: entry: coverage run language: system always_run: true + pass_filenames: false + - id: coverage-combine + name: coverage-combine + stages: [pre-push, manual] + types: [python] + entry: coverage combine + language: system + always_run: true pass_filenames: false \ No newline at end of file diff --git a/day16/lib/cells.py b/day16/lib/cells.py index 10048af..62aa2d7 100644 --- a/day16/lib/cells.py +++ b/day16/lib/cells.py @@ -22,14 +22,11 @@ def construct(contents: str) -> "Cell": return ForwardSlashCell() elif contents == "\\": return BackSlashCell() - raise ValueError(f"unrecognized content {contents}") - - def __init__(self, contents: str): - self.contents = contents + raise AssertionError(f"unrecognized content {contents}") @abstractmethod def next_lasers(self, laser: Laser) -> list[Laser]: - raise ValueError("Not supported", laser) + raise AssertionError("Not supported", laser) class DotCell(Cell): @@ -49,11 +46,13 @@ def next_lasers(self, laser: Laser) -> list[Laser]: if laser.direction in [Direction.EAST, Direction.WEST]: row, col = laser.direction.offset(laser.row, laser.col) return [Laser(row, col, laser.direction)] - row, col = laser.row, laser.col - return [ - Laser(row, col + 1, Direction.EAST), - Laser(row, col - 1, Direction.WEST), - ] + elif laser.direction in [Direction.NORTH, Direction.SOUTH]: + row, col = laser.row, laser.col + return [ + Laser(row, col + 1, Direction.EAST), + Laser(row, col - 1, Direction.WEST), + ] + raise AssertionError(f"Unknown direction {laser.direction}") class PipeCell(Cell): @@ -64,11 +63,13 @@ def next_lasers(self, laser: Laser) -> list[Laser]: if laser.direction in [Direction.NORTH, Direction.SOUTH]: row, col = laser.direction.offset(laser.row, laser.col) return [Laser(row, col, laser.direction)] - row, col = laser.row, laser.col - return [ - Laser(row - 1, col, Direction.NORTH), - Laser(row + 1, col, Direction.SOUTH), - ] + elif laser.direction in [Direction.EAST, Direction.WEST]: + row, col = laser.row, laser.col + return [ + Laser(row - 1, col, Direction.NORTH), + Laser(row + 1, col, Direction.SOUTH), + ] + raise AssertionError(f"Unknown direction {laser.direction}") class ForwardSlashCell(Cell): @@ -85,6 +86,7 @@ def next_lasers(self, laser: Laser) -> list[Laser]: return [Laser(row, col - 1, Direction.WEST)] if laser.direction == Direction.WEST: return [Laser(row + 1, col, Direction.SOUTH)] + raise AssertionError(f"Unknown direction {laser.direction}") class BackSlashCell(Cell): @@ -101,3 +103,4 @@ def next_lasers(self, laser: Laser) -> list[Laser]: return [Laser(row, col - 1, Direction.WEST)] if laser.direction == Direction.WEST: return [Laser(row - 1, col, Direction.NORTH)] + raise AssertionError(f"Unknown direction {laser.direction}") diff --git a/day16/lib/direction.py b/day16/lib/direction.py index 8151253..8f31f6d 100644 --- a/day16/lib/direction.py +++ b/day16/lib/direction.py @@ -28,4 +28,4 @@ def offset(self, row: int, col: int) -> tuple[int, int]: return (row + 1, col) if self == Direction.WEST: return (row, col - 1) - raise ValueError("direction not suppported", self) + raise AssertionError("direction not suppported", self) diff --git a/day16/tests/test_world.py b/day16/tests/test_world.py new file mode 100644 index 0000000..4ccfd6e --- /dev/null +++ b/day16/tests/test_world.py @@ -0,0 +1,31 @@ +from typing import TYPE_CHECKING + +from day16.day16 import INPUT_SMALL +from day16.lib.direction import Direction +from day16.lib.laser import Laser +from day16.lib.parsers import get_input + +if TYPE_CHECKING: + from day16.lib.world import SolvedWorld, World + + +def test_world() -> None: + world: World = get_input(INPUT_SMALL) + start_laser: Laser = Laser(0, 0, Direction.EAST) + solved_world: SolvedWorld = world.solve(start_laser) + + print(solved_world) + assert str(solved_world) == "\n".join( + [ + "1211110000", + "0100010000", + "0100011111", + "0100011000", + "0100011000", + "0100011000", + "0100122100", + "1211111100", + "0111121100", + "0100010100", + ] + ) diff --git a/pyproject.toml b/pyproject.toml index 67fd26c..cb86ad5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,12 +26,12 @@ section-order = [ ] [tool.coverage.run] +concurrency = ["multiprocessing"] branch = true command_line = '-m pytest' source = ['.'] relative_files = true - [tool.coverage.report] exclude_lines = [ "if __name__ == .__main__.:",