Skip to content

Commit

Permalink
day08: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 27, 2023
1 parent 7f699f4 commit 0c53d23
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
14 changes: 9 additions & 5 deletions day08/day8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]


Expand All @@ -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:
Expand All @@ -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]:
Expand Down
7 changes: 7 additions & 0 deletions day08/tests/test_day8.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
INPUT_B,
INPUT_C,
Cycle,
Directions,
Location,
find_cycle,
follow_directions,
Expand Down Expand Up @@ -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"
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c53d23

Please sign in to comment.