diff --git a/src/holt59/aoc/2024/day15.py b/src/holt59/aoc/2024/day15.py index f3a5e86..e5dba3f 100644 --- a/src/holt59/aoc/2024/day15.py +++ b/src/holt59/aoc/2024/day15.py @@ -12,36 +12,38 @@ ImageGrid: TypeAlias = NDArray[np.uint8] class Solver(BaseSolver): - def print_grid(self, grid: Grid, robot: Robot): - if self.verbose: + def print_grid(self, name: str, grid: Grid, robot: Robot): + if self.files: grid = [[c for c in row] for row in grid] grid[robot[0]][robot[1]] = "\033[31;1m@\033[00m" - for row in grid: - self.logger.info("".join(row)) + self.files.create( + name, "\n".join("".join(row) for row in grid).encode(), True + ) def convert_grid(self, grid: Grid, robot: Robot) -> ImageGrid: import numpy as np grid = [[c for c in row] for row in grid] grid[robot[0]][robot[1]] = "@" - return np.array( - [ - [ - # { - # "#": [255, 255, 255], - # "@": [255, 0, 0], - # "O": [0, 255, 0], - # "[": [0, 0, 255], - # "]": [0, 0, 255], - # ".": [0, 0, 0], - # }[col] - ord(col) - for col in row - ] - for row in grid - ], - dtype=np.uint8, - ) + # return np.array( + # [ + # [ + # # { + # # "#": [255, 255, 255], + # # "@": [255, 0, 0], + # # "O": [0, 255, 0], + # # "[": [0, 0, 255], + # # "]": [0, 0, 255], + # # ".": [0, 0, 0], + # # }[col] + # ord(col) + # for col in row + # ] + # for row in grid + # ], + # dtype=np.uint8, + # ) + return np.array(grid) def step_part1(self, grid: Grid, move: str, robot: Robot): match move: @@ -141,6 +143,7 @@ class Solver(BaseSolver): def run( self, + name: str, grid: Grid, moves: str, fn: Callable[ @@ -162,7 +165,7 @@ class Solver(BaseSolver): if generate: images.append(self.convert_grid(grid, robot)) - self.print_grid(grid, robot) + self.print_grid(f"initial_grid_{name}.txt", grid, robot) for move in self.progress.wrap(moves): self.logger.debug(f"Move '{move}'...") @@ -171,7 +174,7 @@ class Solver(BaseSolver): if generate: images.append(self.convert_grid(grid, robot)) - self.print_grid(grid, robot) + self.print_grid(f"final_grid_{name}.txt", grid, robot) return grid, images @@ -180,6 +183,7 @@ class Solver(BaseSolver): moves = "".join(moves.split()) grid, images = self.run( + "part1", [[c for c in r] for r in grid_s.splitlines()], moves, self.step_part1, @@ -193,6 +197,7 @@ class Solver(BaseSolver): ) grid, images = self.run( + "part2", [ list( itertools.chain.from_iterable(