Skip to content

Commit

Permalink
day14: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 27, 2023
1 parent fd9d479 commit 82f499b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
68 changes: 32 additions & 36 deletions day14/day14.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""day14 solution"""

from dataclasses import dataclass
from typing import Any
from typing import Any, Optional

from day14.lib.direction import Direction

Expand Down Expand Up @@ -30,15 +30,10 @@ def __hash__(self) -> int:
def to_string(self) -> str:
return "\n".join(str(row) for row in self.data)

def print_map(self) -> None:
"""prints the map nicely"""
print(self.to_string())

def print_correct(self) -> None:
def as_orientiented_north(self) -> str:
"""Print with north facing up"""
world = self.correct_side()
world.print_map()
print(f"last action:{self.left_is}")
return world.to_string()

def get_score(self) -> int:
world = self.correct_side()
Expand All @@ -56,7 +51,7 @@ def correct_side(self) -> "World":
return world
elif world.left_is == Direction.South:
return world.rotate_world_ccw()
raise ValueError(f"Unsupported Direction: {world.left_is}")
raise AssertionError(f"Unsupported Direction: {world.left_is}")


def naive_score(world_rows: list[str]) -> int:
Expand Down Expand Up @@ -124,32 +119,32 @@ def question2(world: World) -> int:
world = world.rotate_world_ccw()
world.data = simulate_world(world.data)

for cycle in range(10000):
# left_is: north
world = world.rotate_world_cw()
world.data = simulate_world(world.data)

# left is: west
world = world.rotate_world_cw()
world.data = simulate_world(world.data)

# left is: south
world = world.rotate_world_cw()
world.data = simulate_world(world.data)
world.score = world.get_score()
# world.print_correct()
print(cycle, world.score)
# east here
if world in cache:
cycle_start = cache[world]
cycle_end = cycle
break
cache[world] = cycle
cycle_results.append(world)

# left is east
world = world.rotate_world_cw()
world.data = simulate_world(world.data)
cycle_index = 0
cycle_start: Optional[int] = None
cycle_end: Optional[int] = None

while cycle_start is None and cycle_end is None:
for direction in [
Direction.North, # left_is north
Direction.West, # left_is west
Direction.South, # left_is south
Direction.East, # left_is east
]:
if direction == Direction.East: # calculate score *before*
if world in cache:
cycle_start = cache[world]
cycle_end = cycle_index
else:
cache[world] = cycle_index
cycle_results.append(world)
world = world.rotate_world_cw()
world.data = simulate_world(world.data)
world.score = world.get_score()

cycle_index += 1

if cycle_end is None or cycle_start is None:
raise AssertionError("cycle_end and cycle_end should not be None")

cycle_length = cycle_end - cycle_start
print(f"cycle length: {cycle_length}")
Expand All @@ -159,7 +154,8 @@ def question2(world: World) -> int:
result = cycle_results[cycle_start + target]

if result.score is None:
raise ValueError("No score found!")
raise AssertionError("No score found!")

return result.score


Expand Down
8 changes: 8 additions & 0 deletions day14/tests/test_day14.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ def test_rotate_world() -> None:

assert world.rotate_world_cw().data == ARROW_EAST
assert world.rotate_world_cw().left_is == Direction.South

world1 = world.rotate_world_cw().rotate_world_ccw()
world2 = world.rotate_world_ccw()
world3 = world.rotate_world_cw()
world4 = world.rotate_world_cw().rotate_world_cw()
assert world1.as_orientiented_north() == world2.as_orientiented_north()
assert world3.as_orientiented_north() == world4.as_orientiented_north()
assert world4.as_orientiented_north() == world1.as_orientiented_north()

0 comments on commit 82f499b

Please sign in to comment.