


"""simple docstring"""
from __future__ import annotations

from PIL import Image

# Define glider example
SCREAMING_SNAKE_CASE :    int									=				[
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
SCREAMING_SNAKE_CASE :    Union[str, Any]									=				[[0, 1, 0], [0, 1, 0], [0, 1, 0]]






def 		lowercase							(     _snake_case						:      list[list[int]]     )     ->list[list[int]]:
	"""simple docstring"""





	__snake_case	:						Union[str, Any]						=				[]
	for i in range(len(_snake_case     )     ):
		__snake_case	:						str						=				[]
		for j in range(len(cells[i]     )     ):
			# Get the number of live neighbours
			__snake_case	:						List[str]						=				0
			if i > 0 and j > 0:
				neighbour_count += cells[i - 1][j - 1]
			if i > 0:
				neighbour_count += cells[i - 1][j]
			if i > 0 and j < len(cells[i]     ) - 1:
				neighbour_count += cells[i - 1][j + 1]
			if j > 0:
				neighbour_count += cells[i][j - 1]
			if j < len(cells[i]     ) - 1:
				neighbour_count += cells[i][j + 1]
			if i < len(_snake_case     ) - 1 and j > 0:
				neighbour_count += cells[i + 1][j - 1]
			if i < len(_snake_case     ) - 1:
				neighbour_count += cells[i + 1][j]
			if i < len(_snake_case     ) - 1 and j < len(cells[i]     ) - 1:
				neighbour_count += cells[i + 1][j + 1]

			# Rules of the game of life (excerpt from Wikipedia):
			# 1. Any live cell with two or three live neighbours survives.
			# 2. Any dead cell with three live neighbours becomes a live cell.
			# 3. All other live cells die in the next generation.
			#    Similarly, all other dead cells stay dead.
			__snake_case	:						List[Any]						=				cells[i][j] == 1
			if (
			    (alive and 2 <= neighbour_count <= 3)
			    or not alive
			    and neighbour_count == 3
			):
				next_generation_row.append(1     )
			else:
				next_generation_row.append(0     )

		next_generation.append(_snake_case     )
	return next_generation






def 		lowercase							(     _snake_case						:      list[list[int]]     ,   _snake_case						:      int     )     ->list[Image.Image]:
	"""simple docstring"""





	__snake_case	:						Any						=				[]
	for _ in range(_snake_case     ):
		# Create output image
		__snake_case	:						List[str]						=				Image.new('''RGB'''     ,   (len(cells[0]     ), len(_snake_case     ))     )
		__snake_case	:						Optional[Any]						=				img.load()

		# Save cells to image
		for x in range(len(_snake_case     )     ):
			for y in range(len(cells[0]     )     ):
				__snake_case	:						List[str]						=				255 - cells[y][x] * 255
				__snake_case	:						List[str]						=				(colour, colour, colour)

        # Save image
		images.append(_snake_case     )
		__snake_case	:						List[Any]						=				new_generation(_snake_case     )
	return images


if __name__ == "__main__":
			SCREAMING_SNAKE_CASE :    List[Any]									=				generate_images(GLIDER, 16)
			images[0].save("""out.gif""", save_all=True, append_images=images[1:])













def 				lowerCamelCase__      (    __lowerCamelCase			:     Tuple  ,		__lowerCamelCase			:     Union[str, Any]		):
  __UpperCAmelCase							:	Tuple           =  [1]
  for i in range(2  ,		__lowerCamelCase		):
    factorials.append(factorials[-1] * i		)
  assert 0 <= k < factorials[-1] * n, "k out of bounds"

  __UpperCAmelCase							:	Optional[Any]           =  []
  __UpperCAmelCase							:	str           =  list(range(__lowerCamelCase		)		)

  # Find permutation
  while factorials:
    __UpperCAmelCase							:	Any           =  factorials.pop()
    __UpperCAmelCase      , __UpperCAmelCase							:	Optional[Any]           =  divmod(__lowerCamelCase  ,		__lowerCamelCase		)
    permutation.append(elements[number]		)
    elements.remove(elements[number]		)
  permutation.append(elements[0]		)

  return permutation


if __name__ == "__main__":
       import doctest

       doctest.testmod()


