





from typing import TYPE_CHECKING

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


A__:  int       					=					{
    '''configuration_openai''': ['''OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''OpenAIGPTConfig'''],
    '''tokenization_openai''': ['''OpenAIGPTTokenizer'''],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__:  Tuple       					=					['''OpenAIGPTTokenizerFast''']

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__:  List[str]       					=					[
          '''OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''OpenAIGPTDoubleHeadsModel''',
          '''OpenAIGPTForSequenceClassification''',
          '''OpenAIGPTLMHeadModel''',
          '''OpenAIGPTModel''',
          '''OpenAIGPTPreTrainedModel''',
          '''load_tf_weights_in_openai_gpt''',
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__:  List[str]       					=					[
          '''TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''TFOpenAIGPTDoubleHeadsModel''',
          '''TFOpenAIGPTForSequenceClassification''',
          '''TFOpenAIGPTLMHeadModel''',
          '''TFOpenAIGPTMainLayer''',
          '''TFOpenAIGPTModel''',
          '''TFOpenAIGPTPreTrainedModel''',
      ]


if TYPE_CHECKING:
      from .configuration_openai import OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenAIGPTConfig
      from .tokenization_openai import OpenAIGPTTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_openai_fast import OpenAIGPTTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_openai import (
                OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
                OpenAIGPTDoubleHeadsModel,
                OpenAIGPTForSequenceClassification,
                OpenAIGPTLMHeadModel,
                OpenAIGPTModel,
                OpenAIGPTPreTrainedModel,
                load_tf_weights_in_openai_gpt,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_openai import (
                TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFOpenAIGPTDoubleHeadsModel,
                TFOpenAIGPTForSequenceClassification,
                TFOpenAIGPTLMHeadModel,
                TFOpenAIGPTMainLayer,
                TFOpenAIGPTModel,
                TFOpenAIGPTPreTrainedModel,
            )

else:
      import sys

      A__:  Optional[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
A__:  Tuple       					=					[[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_				(    A_):
    return [int(A_) for c in F"{ruleset:08}"[:8]]



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



def 							lowerCAmelCase_				(    A_):
    UpperCamelCase__:					int												=      Image.new("RGB"  ,(len(cells[0]), len(A_)))
    UpperCamelCase__:					Dict												=      img.load()
    # Generates image
    for w in range(img.width):
        for h in range(img.height):
            UpperCamelCase__:					List[str]												=      2_55 - int(2_55 * cells[h][w])
            UpperCamelCase__:					Union[str, Any]												=      (color, color, color)
    return img


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

