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

@@ -83,18 +83,17 @@ class Solver(BaseSolver):
if (i, j) in loop_s and lines[i][j] in "|LJ":
cnt += 1
if self.verbose:
for i in range(len(lines)):
s = ""
for j in range(len(lines[0])):
if (i, j) == (si, sj):
s += "\033[91mS\033[0m"
elif (i, j) in loop:
s += lines[i][j]
elif (i, j) in inside:
s += "\033[92mI\033[0m"
else:
s += "."
self.logger.info(s)
if self.files:
rows = [["." for _j in range(len(lines[0]))] for _i in range(len(lines))]
rows[si][sj] = "\033[91mS\033[0m"
for i, j in loop:
rows[i][j] = lines[i][j]
for i, j in inside:
rows[i][j] = "\033[92mI\033[0m"
self.files.create(
"output.txt", "\n".join("".join(row) for row in rows).encode(), True
)
yield len(inside)

View File

@@ -84,9 +84,14 @@ class Solver(BaseSolver):
beams = propagate(layout, (0, 0), "R")
if self.verbose:
for row in beams:
self.logger.info("".join("#" if col else "." for col in row))
if self.files:
self.files.create(
"beams.txt",
"\n".join(
"".join("#" if col else "." for col in row) for row in beams
).encode(),
True,
)
# part 1
yield sum(sum(map(bool, row)) for row in beams)

View File

@@ -33,10 +33,14 @@ MAPPINGS: dict[Direction, tuple[int, int, Direction]] = {
class Solver(BaseSolver):
def print_shortest_path(
self,
name: str,
grid: list[list[int]],
target: tuple[int, int],
per_cell: dict[tuple[int, int], list[tuple[Label, int]]],
):
if not self.files:
return
assert len(per_cell[target]) == 1
label = per_cell[target][0][0]
@@ -74,8 +78,9 @@ class Solver(BaseSolver):
p_grid[0][0] = f"\033[92m{grid[0][0]}\033[0m"
for row in p_grid:
self.logger.info("".join(row))
self.files.create(
name, "\n".join("".join(row) for row in p_grid).encode(), True
)
def shortest_many_paths(self, grid: list[list[int]]) -> dict[tuple[int, int], int]:
n_rows, n_cols = len(grid), len(grid[0])
@@ -129,6 +134,7 @@ class Solver(BaseSolver):
def shortest_path(
self,
name: str,
grid: list[list[int]],
min_straight: int,
max_straight: int,
@@ -217,8 +223,7 @@ class Solver(BaseSolver):
),
)
if self.verbose:
self.print_shortest_path(grid, target, per_cell)
self.print_shortest_path(f"shortest-path_{name}.txt", grid, target, per_cell)
return per_cell[target][0][1]
@@ -227,7 +232,7 @@ class Solver(BaseSolver):
estimates = self.shortest_many_paths(data)
# part 1
yield self.shortest_path(data, 1, 3, lower_bounds=estimates)
yield self.shortest_path("answer_1", data, 1, 3, lower_bounds=estimates)
# part 2
yield self.shortest_path(data, 4, 10, lower_bounds=estimates)
yield self.shortest_path("answer_2", data, 4, 10, lower_bounds=estimates)

View File

@@ -80,22 +80,23 @@ class Solver(BaseSolver):
outputs,
)
if self.outputs:
with open("./day20.dot", "w") as fp:
fp.write("digraph G {\n")
fp.write("rx [shape=circle, color=red, style=filled];\n")
for name, (type, outputs) in self._modules.items():
if type == "conjunction":
shape = "diamond"
elif type == "flip-flop":
shape = "box"
else:
shape = "circle"
fp.write(f"{name} [shape={shape}];\n")
for name, (type, outputs) in self._modules.items():
for output in outputs:
fp.write(f"{name} -> {output};\n")
fp.write("}\n")
if self.files:
contents = "digraph G {\n"
contents += "rx [shape=circle, color=red, style=filled];\n"
for name, (type, outputs) in self._modules.items():
if type == "conjunction":
shape = "diamond"
elif type == "flip-flop":
shape = "box"
else:
shape = "circle"
contents += f"{name} [shape={shape}];\n"
for name, (type, outputs) in self._modules.items():
for output in outputs:
contents += f"{name} -> {output};\n"
contents += "}\n"
self.files.create("day20.dot", contents.encode(), False)
# part 1
flip_flop_states: dict[str, Literal["on", "off"]] = {

View File

@@ -50,7 +50,7 @@ class Solver(BaseSolver):
values.append(len(tiles := reachable(map, tiles, cycle)))
values.append(len(tiles := reachable(map, tiles, cycle)))
if self.verbose:
if self.files:
n_rows, n_cols = len(map), len(map[0])
rows = [
@@ -66,8 +66,9 @@ class Solver(BaseSolver):
if (i // cycle) % 2 == (j // cycle) % 2:
rows[i][j] = f"\033[91m{rows[i][j]}\033[0m"
for row in rows:
self.logger.info("".join(row))
self.files.create(
"cycle.txt", "\n".join("".join(row) for row in rows).encode(), True
)
self.logger.info(f"values to fit: {values}")