





import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union




class    _a  (  enum.Enum):

      """simple docstring"""


      UpperCamelCase__       						=   """timm"""
      UpperCamelCase__       						=   """transformers"""



def 							lowerCAmelCase_				(    A_  ,A_  ,A_):
    if stage_names is None:
        raise ValueError("Stage_names must be set for transformers backbones")

    if out_features is not None:
        if not isinstance(A_  ,(list,)):
            raise ValueError(F"out_features must be a list {type(A_)}")
        if any(feat not in stage_names for feat in out_features):
            raise ValueError(F"out_features must be a subset of stage_names: {stage_names} got {out_features}")

    if out_indices is not None:
        if not isinstance(A_  ,(list, tuple)):
            raise ValueError(F"out_indices must be a list or tuple, got {type(A_)}")
        if any(idx >= len(A_) for idx in out_indices):
            raise ValueError("out_indices must be valid indices for stage_names {stage_names}, got {out_indices}")

    if out_features is not None and out_indices is not None:
        if len(A_) != len(A_):
            raise ValueError("out_features and out_indices should have the same length if both are set")
        if out_features != [stage_names[idx] for idx in out_indices]:
            raise ValueError("out_features and out_indices should correspond to the same stages if both are set")



def 							lowerCAmelCase_				(    A_  ,A_  ,A_  ,):
    if out_indices is None and out_features is None:
        UpperCamelCase__:					Any												=      [len(A_) - 1]
        UpperCamelCase__:					List[Any]												=      [stage_names[-1]]
    elif out_indices is None and out_features is not None:
        UpperCamelCase__:					Tuple												=      [stage_names.index(A_) for layer in out_features]
    elif out_features is None and out_indices is not None:
        UpperCamelCase__:					List[str]												=      [stage_names[idx] for idx in out_indices]
    return out_features, out_indices



def 							lowerCAmelCase_				(    A_  ,A_  ,A_  ,):
    verify_out_features_out_indices(out_features=A_  ,out_indices=A_  ,stage_names=A_)
    UpperCamelCase__:					str												=      _align_output_features_output_indices(
        out_features=A_  ,out_indices=A_  ,stage_names=A_)
    # Verify that the aligned out_features and out_indices are valid
    verify_out_features_out_indices(out_features=A_  ,out_indices=A_  ,stage_names=A_)
    return output_features, output_indices




class    _a  :

      """simple docstring"""


      UpperCamelCase__       						=   None






      def 	UpperCAmelCase_   (						self:	str					,  __lowerCamelCase:	Dict			):
          '''simple docstring'''


          if getattr(self					,  "_backbone"					,  __lowerCamelCase			) is None:
              raise ValueError("self._backbone must be set before calling _init_timm_backbone"			)

          # These will diagree with the defaults for the transformers models e.g. for resnet50
          # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
          # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
          UpperCamelCase__:					int												=      [stage["module"] for stage in self._backbone.feature_info.info]
          UpperCamelCase__:					int												=      [stage["num_chs"] for stage in self._backbone.feature_info.info]
          UpperCamelCase__:					Dict												=      self._backbone.feature_info.out_indices
          UpperCamelCase__:					Any												=      self._backbone.feature_info.module_name()

          # We verify the out indices and out features are valid
          verify_out_features_out_indices(
              out_features=__lowerCamelCase					,  out_indices=__lowerCamelCase					,  stage_names=self.stage_names			)
          UpperCamelCase__:					List[str]												=      out_features, out_indices






      def 	UpperCAmelCase_   (						self:	Optional[Any]					,  __lowerCamelCase:	str			):
          '''simple docstring'''


          UpperCamelCase__:					Tuple												=      getattr(__lowerCamelCase					,  "stage_names"			)
          UpperCamelCase__:					Tuple												=      getattr(__lowerCamelCase					,  "out_features"					,  __lowerCamelCase			)
          UpperCamelCase__:					Union[str, Any]												=      getattr(__lowerCamelCase					,  "out_indices"					,  __lowerCamelCase			)

          UpperCamelCase__:					int												=      stage_names
          UpperCamelCase__:					Any												=      get_aligned_output_features_output_indices(
              out_features=__lowerCamelCase					,  out_indices=__lowerCamelCase					,  stage_names=__lowerCamelCase			)
          # Number of channels for each stage. This is set in the transformer backbone model init
          UpperCamelCase__:					str												=      None






      def 	UpperCAmelCase_   (						self:	Union[str, Any]					,  __lowerCamelCase:	List[Any]			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[Any]												=      config

          UpperCamelCase__:					Dict												=      getattr(__lowerCamelCase					,  "use_timm_backbone"					,  __lowerCamelCase			)
          UpperCamelCase__:					Any												=      BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

          if self.backbone_type == BackboneType.TIMM:
              self._init_timm_backbone(__lowerCamelCase			)
          elif self.backbone_type == BackboneType.TRANSFORMERS:
              self._init_transformers_backbone(__lowerCamelCase			)
          else:
              raise ValueError(F"backbone_type {self.backbone_type} not supported."			)






      @property
      def 	UpperCAmelCase_   (						self:	List[Any]			):
          '''simple docstring'''


          return self._out_features






      @out_features.setter
      def 	UpperCAmelCase_   (						self:	Dict					,  __lowerCamelCase:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					List[Any]												=      get_aligned_output_features_output_indices(
              out_features=__lowerCamelCase					,  out_indices=__lowerCamelCase					,  stage_names=self.stage_names			)






      @property
      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          return self._out_indices






      @out_indices.setter
      def 	UpperCAmelCase_   (						self:	Optional[Any]					,  __lowerCamelCase:	Union[Tuple[int], List[int]]			):
          '''simple docstring'''


          UpperCamelCase__:					Tuple												=      get_aligned_output_features_output_indices(
              out_features=__lowerCamelCase					,  out_indices=__lowerCamelCase					,  stage_names=self.stage_names			)






      @property
      def 	UpperCAmelCase_   (						self:	Optional[Any]			):
          '''simple docstring'''


          return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names			)}






      @property
      def 	UpperCAmelCase_   (						self:	Tuple			):
          '''simple docstring'''


          return [self.out_feature_channels[name] for name in self.out_features]






      def 	UpperCAmelCase_   (						self:	Union[str, Any]					,  *__lowerCamelCase:	str					,  **__lowerCamelCase:	Any			):
          '''simple docstring'''


          UpperCamelCase__:					str												=      dict(inspect.signature(self.forward			).parameters			)
          UpperCamelCase__:					Optional[Any]												=      {k: v for k, v in kwargs.items() if k in signature}
          return self(*__lowerCamelCase					,  **__lowerCamelCase			)






      def 	UpperCAmelCase_   (						self:	Optional[int]					,  __lowerCamelCase:	Dict					,  __lowerCamelCase:	Optional[bool] = None					,  __lowerCamelCase:	Optional[bool] = None					,  __lowerCamelCase:	Optional[bool] = None					,  ):
          '''simple docstring'''


          raise NotImplementedError("This method should be implemented by the derived class."			)






      def 	UpperCAmelCase_   (						self:	Optional[Any]			):
          '''simple docstring'''


          UpperCamelCase__:					Union[str, Any]												=      super().to_dict()
          UpperCamelCase__:					Any												=      output.pop("_out_features"			)
          UpperCamelCase__:					int												=      output.pop("_out_indices"			)
          return output




class    _a  :

      """simple docstring"""


      @property
      def 	UpperCAmelCase_   (						self:	Any			):
          '''simple docstring'''


          return self._out_features






      @out_features.setter
      def 	UpperCAmelCase_   (						self:	Tuple					,  __lowerCamelCase:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					int												=      get_aligned_output_features_output_indices(
              out_features=__lowerCamelCase					,  out_indices=__lowerCamelCase					,  stage_names=self.stage_names			)






      @property
      def 	UpperCAmelCase_   (						self:	List[Any]			):
          '''simple docstring'''


          return self._out_indices






      @out_indices.setter
      def 	UpperCAmelCase_   (						self:	Optional[int]					,  __lowerCamelCase:	Union[Tuple[int], List[int]]			):
          '''simple docstring'''


          UpperCamelCase__:					List[str]												=      get_aligned_output_features_output_indices(
              out_features=__lowerCamelCase					,  out_indices=__lowerCamelCase					,  stage_names=self.stage_names			)






      def 	UpperCAmelCase_   (						self:	Tuple			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      super().to_dict()
          UpperCamelCase__:					Any												=      output.pop("_out_features"			)
          UpperCamelCase__:					List[Any]												=      output.pop("_out_indices"			)
          return output








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()

