


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


_UpperCAmelCase      :	int  ={"""configuration_gptj""": ["""GPTJ_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTJConfig""", """GPTJOnnxConfig"""]}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_UpperCAmelCase      :	Union[str, Any]  =[
						    """GPTJ_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """GPTJForCausalLM""",
						    """GPTJForQuestionAnswering""",
						    """GPTJForSequenceClassification""",
						    """GPTJModel""",
						    """GPTJPreTrainedModel""",
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_UpperCAmelCase      :	Dict  =[
						    """TFGPTJForCausalLM""",
						    """TFGPTJForQuestionAnswering""",
						    """TFGPTJForSequenceClassification""",
						    """TFGPTJModel""",
						    """TFGPTJPreTrainedModel""",
						]

try:
						if not is_flax_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_UpperCAmelCase      :	int  =[
						    """FlaxGPTJForCausalLM""",
						    """FlaxGPTJModel""",
						    """FlaxGPTJPreTrainedModel""",
						]


if TYPE_CHECKING:
						from .configuration_gptj import GPTJ_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTJConfig, GPTJOnnxConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_gptj import (
												    GPTJ_PRETRAINED_MODEL_ARCHIVE_LIST,
												    GPTJForCausalLM,
												    GPTJForQuestionAnswering,
												    GPTJForSequenceClassification,
												    GPTJModel,
												    GPTJPreTrainedModel,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_gptj import (
												    TFGPTJForCausalLM,
												    TFGPTJForQuestionAnswering,
												    TFGPTJForSequenceClassification,
												    TFGPTJModel,
												    TFGPTJPreTrainedModel,
												)

						try:
												if not is_flax_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel, FlaxGPTJPreTrainedModel

else:
						import sys

						_UpperCAmelCase      :	Any  =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
_UpperCAmelCase      :	int  =[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]



def       lowerCAmelCase			(		lowerCAmelCase_			)->			list[int]:
				return [int(lowerCAmelCase_			) for c in f"""{ruleset:08}"""[:8]]



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)->			list[int]:
				lowerCAmelCase_			:   Optional[Any]     					=					len(cells[0]			)  # 31
				lowerCAmelCase_			:   int     					=					[]
				for i in range(lowerCAmelCase_			):
								# Get the neighbors of each cell
								# Handle neighbours outside bounds by using 0 as their value
								lowerCAmelCase_			:   Union[str, Any]     					=					0 if i == 0 else cells[time][i - 1]
								lowerCAmelCase_			:   List[str]     					=					0 if i == population - 1 else cells[time][i + 1]
								# Define a new cell and add it to the new generation
								lowerCAmelCase_			:   List[Any]     					=					7 - int(f"""{left_neighbor}{cells[time][i]}{right_neighbor}"""			, 2			)
								next_generation.append(rule[situation]			)
				return next_generation



def       lowerCAmelCase			(		lowerCAmelCase_			)->			Image.Image:
				lowerCAmelCase_			:   Optional[Any]     					=					Image.new('''RGB'''			, (len(cells[0]			), len(lowerCAmelCase_			))			)
				lowerCAmelCase_			:   str     					=					img.load()
				# Generates image
				for w in range(img.width			):
								for h in range(img.height			):
												lowerCAmelCase_			:   Optional[Any]     					=					255 - int(255 * cells[h][w]			)
												lowerCAmelCase_			:   List[Any]     					=					(color, color, color)
				return img


if __name__ == "__main__":
						_UpperCAmelCase      :	str  =bin(int(input("""Rule:\n""").strip()))[2:]
						_UpperCAmelCase      :	List[str]  =format_ruleset(int(rule_num))
						for time in range(16):
												CELLS.append(new_generation(CELLS, rule, time))
						_UpperCAmelCase      :	Dict  =generate_image(CELLS)
						# Uncomment to save the image
						# img.save(f"rule_{rule_num}.png")
						img.show()