import time
from dataclasses import dataclass

@dataclass
class SearchStats:
    timeMs: float
    visitedCells: int
    pathLength: int
    path: list


class MazeSolver:
    def __init__(self, maze, strategy):
        self.maze = maze
        self.strategy = strategy

    def setStrategy(self, strategy):
        self.strategy = strategy

    def solve(self) -> SearchStats:
        if self.maze.start is None or self.maze.exit is None:
            raise ValueError("Лабиринт должен содержать start и exit")

        t0 = time.perf_counter()
        result = self.strategy.findPath(self.maze, self.maze.start, self.maze.exit)
        t1 = time.perf_counter()

        if isinstance(result, tuple):
            path, visited = result
        else:
            path, visited = result, 0

        return SearchStats(
            timeMs=(t1 - t0) * 1000,
            visitedCells=visited,
            pathLength=len(path),
            path=path
        )