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

This commit is contained in:
Mikaël Capelle 2024-12-15 11:39:58 +01:00
parent 2c25b33bcc
commit bc06f86fdc

View File

@ -13,12 +13,13 @@ ImageGrid: TypeAlias = NDArray[np.uint8]
class BoxUtils: class BoxUtils:
FREE: Final = 0 FREE: Final = 0
BLOCK: Final = 1 BLOCK: Final = 1
ROBOT: Final = 2
@staticmethod @staticmethod
def convert(grid_s: list[str], large: bool): def convert(grid_s: list[str], large: bool):
grid: list[list[int]] = [] grid: list[list[int]] = []
robot: tuple[int, int] | None = None 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): for i_row, row in enumerate(grid_s):
row_u: list[int] = [] row_u: list[int] = []
for i_col, col in enumerate(row): for i_col, col in enumerate(row):
@ -42,15 +43,15 @@ class BoxUtils:
@staticmethod @staticmethod
def is_box(c: int): def is_box(c: int):
return c >= 2 and c % 2 == 0 return c >= 4 and c % 2 == 0
@staticmethod @staticmethod
def is_open_or_close_box(c: int): 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 @staticmethod
def is_open_box(c: int): def is_open_box(c: int):
return c >= 2 and c % 2 == 1 return c >= 4 and c % 2 == 1
@staticmethod @staticmethod
def is_close_box(c: int): def is_close_box(c: int):
@ -101,7 +102,9 @@ class Solver(BaseSolver):
# ], # ],
# dtype=np.uint8, # 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): def step_part1(self, grid: Grid, move: str, robot: Robot):
match move: match move:
@ -228,6 +231,18 @@ class Solver(BaseSolver):
grid_s, moves = input.split("\n\n") grid_s, moves = input.split("\n\n")
moves = "".join(moves.split()) 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( grid, images = self.run(
"part1", "part1",
*BoxUtils.convert(grid_s.splitlines(), False), *BoxUtils.convert(grid_s.splitlines(), False),
@ -236,10 +251,9 @@ class Solver(BaseSolver):
self.files is not None, self.files is not None,
) )
if self.files: if self.files:
print(images[0].max()) images = np.stack(images, axis=0)
self.files.image( images[images >= 2] = 1 + images[images >= 2] // 2
"anim_part1.gif", np.stack(images, axis=0).astype(np.uint8) self.files.image("anim_part1.gif", colors[images])
)
yield sum( yield sum(
100 * i_row + i_col 100 * i_row + i_col
for i_row, row in enumerate(grid) for i_row, row in enumerate(grid)
@ -255,9 +269,9 @@ class Solver(BaseSolver):
self.files is not None, self.files is not None,
) )
if self.files: if self.files:
self.files.image( images = np.abs(np.stack(images, axis=0))
"anim_part2.gif", np.stack(images, axis=0).astype(np.uint8) images[images >= 2] = 1 + images[images >= 2] // 2
) self.files.image("anim_part2.gif", colors[images])
yield sum( yield sum(
100 * i_row + i_col 100 * i_row + i_col
for i_row, row in enumerate(grid) for i_row, row in enumerate(grid)