diff --git a/src/holt59/aoc/2024/day15.py b/src/holt59/aoc/2024/day15.py index 67cbc7a..502ad9b 100644 --- a/src/holt59/aoc/2024/day15.py +++ b/src/holt59/aoc/2024/day15.py @@ -13,12 +13,13 @@ ImageGrid: TypeAlias = NDArray[np.uint8] class BoxUtils: FREE: Final = 0 BLOCK: Final = 1 + ROBOT: Final = 2 @staticmethod def convert(grid_s: list[str], large: bool): grid: list[list[int]] = [] robot: tuple[int, int] | None = None - box_counter = 2 if not large else 3 + box_counter = 4 if not large else 5 for i_row, row in enumerate(grid_s): row_u: list[int] = [] for i_col, col in enumerate(row): @@ -42,15 +43,15 @@ class BoxUtils: @staticmethod def is_box(c: int): - return c >= 2 and c % 2 == 0 + return c >= 4 and c % 2 == 0 @staticmethod def is_open_or_close_box(c: int): - return abs(c) >= 2 and c % 2 == 1 + return abs(c) >= 4 and c % 2 == 1 @staticmethod def is_open_box(c: int): - return c >= 2 and c % 2 == 1 + return c >= 4 and c % 2 == 1 @staticmethod def is_close_box(c: int): @@ -101,7 +102,9 @@ class Solver(BaseSolver): # ], # dtype=np.uint8, # ) - return np.abs(np.array(grid)) + arr = np.array(grid) + arr[*robot] = BoxUtils.ROBOT + return arr def step_part1(self, grid: Grid, move: str, robot: Robot): match move: @@ -228,6 +231,18 @@ class Solver(BaseSolver): grid_s, moves = input.split("\n\n") moves = "".join(moves.split()) + n_boxes = grid_s.count("O") + colors = np.concatenate( + [ + np.array( + [[255, 255, 255], [64, 64, 64], [255, 0, 0]], + dtype=np.uint8, + ), + np.random.randint(0, 256, size=(n_boxes, 3), dtype=np.uint8), + ], + dtype=np.uint8, + ) + grid, images = self.run( "part1", *BoxUtils.convert(grid_s.splitlines(), False), @@ -236,10 +251,9 @@ class Solver(BaseSolver): self.files is not None, ) if self.files: - print(images[0].max()) - self.files.image( - "anim_part1.gif", np.stack(images, axis=0).astype(np.uint8) - ) + images = np.stack(images, axis=0) + images[images >= 2] = 1 + images[images >= 2] // 2 + self.files.image("anim_part1.gif", colors[images]) yield sum( 100 * i_row + i_col for i_row, row in enumerate(grid) @@ -255,9 +269,9 @@ class Solver(BaseSolver): self.files is not None, ) if self.files: - self.files.image( - "anim_part2.gif", np.stack(images, axis=0).astype(np.uint8) - ) + images = np.abs(np.stack(images, axis=0)) + images[images >= 2] = 1 + images[images >= 2] // 2 + self.files.image("anim_part2.gif", colors[images]) yield sum( 100 * i_row + i_col for i_row, row in enumerate(grid)