Skip to content

Commit

Permalink
day16: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 27, 2023
1 parent 880102a commit 80a0810
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 18 additions & 15 deletions day16/lib/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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}")
2 changes: 1 addition & 1 deletion day16/lib/direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
31 changes: 31 additions & 0 deletions day16/tests/test_world.py
Original file line number Diff line number Diff line change
@@ -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",
]
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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__.:",
Expand Down

0 comments on commit 80a0810

Please sign in to comment.