def test_step_small(): grid0 = [ [1, 1, 1, 1, 1], [1, 9, 9, 9, 1], [1, 9, 1, 9, 1], [1, 9, 9, 9, 1], [1, 1, 1, 1, 1], ] flashes1 = 9 grid1 = [ [3, 4, 5, 4, 3], [4, 0, 0, 0, 4], [5, 0, 0, 0, 5], [4, 0, 0, 0, 4], [3, 4, 5, 4, 3], ] flashes2 = 0 grid2 = [ [4, 5, 6, 5, 4], [5, 1, 1, 1, 5], [6, 1, 1, 1, 6], [5, 1, 1, 1, 5], [4, 5, 6, 5, 4], ] actual1, actual_flash1 = step(grid0) # Need to deep compare lists for actual_row, grid1_row in zip(actual1, grid1): assert actual_row == grid1_row assert actual_flash1 == flashes1 actual2, actual_flash2 = step(grid1) # Need to deep compare lists for actual_row, grid2_row in zip(actual2, grid2): assert actual_row == grid2_row assert actual_flash2 == flashes2
def test_cells_expand_each_step(self): cells1 = [[(0, 0)]] cells2 = step(cells1) cells3 = step(cells2) self.assertItemsEqual([(0, 0), (-1, 0), (1, 0), (0, 1), (0, -1)], cells2[0]) self.assertItemsEqual([(0, 0), (-1, 0), (1, 0), (0, 1), (0, -1), (-2, 0), (2, 0), (0, 2), (0, -2), (-1, -1), (-1, 1), (1, -1), (1, 1)], cells3[0])
def test_step(steps, expected): template = list("NNCB") recipes = { ("C", "H"): "B", ("H", "H"): "N", ("C", "B"): "H", ("N", "H"): "C", ("H", "B"): "C", ("H", "C"): "B", ("H", "N"): "C", ("N", "N"): "C", ("B", "H"): "H", ("N", "C"): "B", ("N", "B"): "B", ("B", "N"): "B", ("B", "B"): "N", ("B", "C"): "B", ("C", "C"): "N", ("C", "N"): "C", } assert step(template, recipes, steps) == list(expected)
def find_sync_step(grid: list[list[int]], max_steps: int = 1000) -> int: for i in range(max_steps): grid, flashes = step(grid) if flashes == len(grid) * len(grid[0]): return i + 1 raise ValueError(f"Sync point not found within {max_steps} steps")
def test_two_cells_of_different_familes_expand(self): cells1 = [[(0, 0)], [(2, 0)]] cells2 = step(cells1) self.assertItemsEqual([(0, 0), (-1, 0), (0, 1), (0, -1)], cells2[0]) self.assertItemsEqual([(2, 0), (2, 1), (2, -1), (3, 0)], cells2[1])