File handling for API.

This commit is contained in:
Mikael CAPELLE
2024-12-10 15:38:00 +01:00
parent 3c544c559b
commit 46558672e8
18 changed files with 288 additions and 160 deletions

View File

@@ -97,7 +97,12 @@ def neighbors(
class Solver(BaseSolver):
def print_path(self, path: list[tuple[int, int]], n_rows: int, n_cols: int) -> None:
def print_path(
self, name: str, path: list[tuple[int, int]], n_rows: int, n_cols: int
) -> None:
if not self.files:
return
end = path[-1]
graph = [["." for _c in range(n_cols)] for _r in range(n_rows)]
@@ -118,8 +123,11 @@ class Solver(BaseSolver):
else:
assert False, "{} -> {} infeasible".format(path[i], path[i + 1])
for row in graph:
self.logger.info("".join(row))
self.files.create(
f"graph_{name}.txt",
"\n".join("".join(row) for row in graph).encode(),
text=True,
)
def solve(self, input: str) -> Iterator[Any]:
lines = input.splitlines()
@@ -157,7 +165,7 @@ class Solver(BaseSolver):
path_1 = make_path(parents_1, start, end)
assert path_1 is not None
self.print_path(path_1, n_rows=len(grid), n_cols=len(grid[0]))
self.print_path("answer1", path_1, n_rows=len(grid), n_cols=len(grid[0]))
yield lengths_1[end] - 1
lengths_2, _ = dijkstra(