From 0c53d23b6ccb4f68d7681f1b0e084fab77f4219d Mon Sep 17 00:00:00 2001 From: alexo Date: Wed, 27 Dec 2023 12:04:27 +1100 Subject: [PATCH] day08: coverage --- day08/day8.py | 14 +++++++++----- day08/tests/test_day8.py | 7 +++++++ pyproject.toml | 7 ++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/day08/day8.py b/day08/day8.py index 43cc6c2..2ffe309 100644 --- a/day08/day8.py +++ b/day08/day8.py @@ -2,6 +2,7 @@ import itertools import math from dataclasses import dataclass, field +from typing import Iterator INPUT = "day08/input.txt" INPUT_A = "day08/input-a.txt" @@ -56,6 +57,10 @@ class Directions: def get_step(self, index: int) -> str: return self.steps[index % len(self.steps)] + def get_steps_iterator(self) -> Iterator[str]: + """Returns a iterator that loops through indefinitely.""" + return itertools.cycle(self.steps) + @dataclass class Cycle: @@ -87,10 +92,9 @@ def get_location(self, index: int) -> LocationStep: # 2nd half of array is from cycle_start_index -> end index -= len(self.location_steps) - index %= self.cycle_length index += self.cycle_start_index - if index >= len(self.location_steps): - raise ValueError("you fucked up") + index %= self.cycle_length + return self.location_steps[index] @@ -117,7 +121,7 @@ def follow_directions(directions: Directions, world_map: WorldMap) -> int: mappings = world_map.mappings node: Location = mappings["AAA"] nodes_visited = 0 - for step in itertools.cycle(directions.steps): + for step in directions.get_steps_iterator(): if step == "L": node = mappings[node.left] else: @@ -126,7 +130,7 @@ def follow_directions(directions: Directions, world_map: WorldMap) -> int: if node.name == "ZZZ": return nodes_visited - return -1 + raise AssertionError("Unreachable; iterator is infinite") def get_location_as(world_map: WorldMap) -> list[Location]: diff --git a/day08/tests/test_day8.py b/day08/tests/test_day8.py index 49f6e86..ac6fd09 100644 --- a/day08/tests/test_day8.py +++ b/day08/tests/test_day8.py @@ -3,6 +3,7 @@ INPUT_B, INPUT_C, Cycle, + Directions, Location, find_cycle, follow_directions, @@ -43,3 +44,9 @@ def test_find_cycle() -> None: cycle_22A: Cycle = find_cycle(location_22A, world_map, directions) assert cycle_11A.cycle_length == 2 assert cycle_22A.cycle_length == 6 + + +def test_directions() -> None: + directions: Directions = Directions("LRLRLLR") + assert directions.get_step(0) == "L" + assert directions.get_step(7) == "L" diff --git a/pyproject.toml b/pyproject.toml index b6c7654..dc1a74a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,12 @@ relative_files = true [tool.coverage.report] -exclude_lines = ["if __name__ == .__main__.:", "def main", "if TYPE_CHECKING:"] +exclude_lines = [ + "if __name__ == .__main__.:", + "def main", + "if TYPE_CHECKING:", + "raise AssertionError", +] omit = ["download_inputs.py", "maker.py"] precision = 2 skip_covered = true