From 14fdb6d16614f5cfc0754853502b4a981bd469d8 Mon Sep 17 00:00:00 2001 From: Prometheus3375 <35541026+Prometheus3375@users.noreply.github.com> Date: Fri, 19 Jul 2024 05:12:56 +0300 Subject: [PATCH] [states] Add solving method --- solve/solver.py | 41 ----------------------------------------- solve/states/base.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 41 deletions(-) delete mode 100644 solve/solver.py diff --git a/solve/solver.py b/solve/solver.py deleted file mode 100644 index 8fe72c0..0000000 --- a/solve/solver.py +++ /dev/null @@ -1,41 +0,0 @@ -from .states import * - - -def solve_state[S, M]( - initial_state: StateWithAllPositions[S, M], - /, - is_doing_triumph: bool, - last_position_touched: str | None = None, - ) -> StateWithAllPositions[S, M]: - """ - Makes moves starting from the given initial state until one of the next states is done. - """ - # region First cycle - if is_doing_triumph and last_position_touched: - states = [ - next_state - for next_state in initial_state.next_states(is_doing_triumph) - if last_position_touched != next_state.first_position - ] - else: - states = list(initial_state.next_states(is_doing_triumph)) - - # endregion - - for _ in range(initial_state.max_cycles - 1): - states = [ - next_state - for state in states - for next_state in state.next_states(is_doing_triumph) - ] - for state in states: - if state.is_done: - return state - else: - raise ValueError( - f'cannot solve encounter with initial {initial_state} ' - f'within {initial_state.max_cycles} cycles' - ) - - -__all__ = 'solve_state', diff --git a/solve/states/base.py b/solve/states/base.py index 18a83df..c5e4f72 100644 --- a/solve/states/base.py +++ b/solve/states/base.py @@ -165,6 +165,38 @@ def next_states(self, /, is_doing_triumph: bool) -> Iterator[Self]: """ raise NotImplementedError + def solve(self, /, is_doing_triumph: bool, last_position_touched: str | None) -> Self: + """ + Makes moves starting from this state until one of the next states is done, + then returns that done state. + """ + # region First cycle + if is_doing_triumph and last_position_touched: + states = [ + next_state + for next_state in self.next_states(is_doing_triumph) + if last_position_touched != next_state.first_position + ] + else: + states = list(self.next_states(is_doing_triumph)) + + # endregion + + for _ in range(self.max_cycles - 1): + states = [ + next_state + for state in states + for next_state in state.next_states(is_doing_triumph) + ] + for state in states: + if state.is_done: + return state + else: + raise ValueError( + f'cannot solve encounter with initial {self} ' + f'within {self.max_cycles} cycles' + ) + def __repr__(self, /) -> str: return ( f'{self.__class__.__name__}('