2024 day 15.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2024-12-15 10:56:28 +01:00
parent 3e8d796b2e
commit 7447c7b536

View File

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