Skip to content

Commit

Permalink
[solver] Use max_cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometheus3375 committed Jul 17, 2024
1 parent 1a2b860 commit 9786418
Showing 1 changed file with 5 additions and 35 deletions.
40 changes: 5 additions & 35 deletions solve/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,28 @@

from .states import *

LIMIT_ROOMS = 6
LIMIT_STATUES = 3


def solve_rooms(
initial_state: StateOfAllRooms,
/,
is_doing_triumph: bool,
last_position_touched: PositionsType | None,
) -> list[PassMove]:
"""
A convenience function to solve all solo rooms.
"""
final = solve_state(initial_state, LIMIT_ROOMS, is_doing_triumph, last_position_touched)
return list(final.moves_made)


def solve_statues(
initial_state: StateOfAllStatues,
/,
is_doing_triumph: bool,
last_position_touched: PositionsType | None,
) -> list[DissectMove]:
"""
A convenience function to solve all statues.
"""
final = solve_state(initial_state, LIMIT_STATUES, is_doing_triumph, last_position_touched)
return list(final.moves_made)


def solve_state[S, M](
initial_state: StateWithAllPositions[S, M],
move_limit: int,
/,
is_doing_triumph: bool,
last_position_touched: str | None = None,
) -> StateWithAllPositions[S, M]:
"""
Makes moves from initial state until one of the next state is completed.
The number of moves are limited by argument ``move_limit``.
Makes moves stating from the given initial state until one of the states is completed.
"""
max_cycles = initial_state.max_cycles
states = [initial_state]
for _ in range(move_limit):
for _ in range(max_cycles):
states = list(chain.from_iterable(s.next_states(is_doing_triumph) for s in states))
for s in states:
if s.is_done and (not is_doing_triumph or last_position_touched != s.first_position):
return s
else:
raise ValueError(
f'cannot solve encounter with initial {initial_state} '
f'within {move_limit} moves'
f'within {max_cycles} cycles'
)


__all__ = 'solve_rooms', 'solve_statues', 'solve_state'
__all__ = 'solve_state',

0 comments on commit 9786418

Please sign in to comment.