File handling for API.
This commit is contained in:
@@ -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(
|
||||
|
@@ -67,13 +67,16 @@ def flow(
|
||||
|
||||
|
||||
class Solver(BaseSolver):
|
||||
def print_blocks(self, blocks: dict[tuple[int, int], Cell]):
|
||||
def print_blocks(self, name: str, blocks: dict[tuple[int, int], Cell]):
|
||||
"""
|
||||
Print the given set of blocks on a grid.
|
||||
|
||||
Args:
|
||||
blocks: Set of blocks to print.
|
||||
"""
|
||||
if not self.files:
|
||||
return
|
||||
|
||||
x_min, y_min, x_max, y_max = (
|
||||
min(x for x, _ in blocks),
|
||||
0,
|
||||
@@ -81,12 +84,16 @@ class Solver(BaseSolver):
|
||||
max(y for _, y in blocks),
|
||||
)
|
||||
|
||||
for y in range(y_min, y_max + 1):
|
||||
self.logger.info(
|
||||
self.files.create(
|
||||
f"blocks_{name}.txt",
|
||||
"\n".join(
|
||||
"".join(
|
||||
str(blocks.get((x, y), Cell.AIR)) for x in range(x_min, x_max + 1)
|
||||
)
|
||||
)
|
||||
for y in range(y_min, y_max + 1)
|
||||
).encode(),
|
||||
True,
|
||||
)
|
||||
|
||||
def solve(self, input: str) -> Iterator[Any]:
|
||||
lines = [line.strip() for line in input.splitlines()]
|
||||
@@ -115,7 +122,7 @@ class Solver(BaseSolver):
|
||||
for y in range(y_start, y_end):
|
||||
blocks[x, y] = Cell.ROCK
|
||||
|
||||
self.print_blocks(blocks)
|
||||
self.print_blocks("start", blocks)
|
||||
|
||||
y_max = max(y for _, y in blocks)
|
||||
|
||||
@@ -124,7 +131,7 @@ class Solver(BaseSolver):
|
||||
blocks_1 = flow(
|
||||
blocks.copy(), stop_fn=lambda x, y: y > y_max, fill_fn=lambda x, y: Cell.AIR
|
||||
)
|
||||
self.print_blocks(blocks_1)
|
||||
self.print_blocks("part1", blocks_1)
|
||||
yield sum(v == Cell.SAND for v in blocks_1.values())
|
||||
|
||||
# === part 2 ===
|
||||
@@ -135,5 +142,5 @@ class Solver(BaseSolver):
|
||||
fill_fn=lambda x, y: Cell.AIR if y < y_max + 2 else Cell.ROCK,
|
||||
)
|
||||
blocks_2[500, 0] = Cell.SAND
|
||||
self.print_blocks(blocks_2)
|
||||
self.print_blocks("part2", blocks_2)
|
||||
yield sum(v == Cell.SAND for v in blocks_2.values())
|
||||
|
Reference in New Issue
Block a user