

'''simple docstring'''

from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
UpperCAmelCase				   =  [[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 				__UpperCamelCase							(	lowercase__						:  int ):






     '''simple docstring'''





     return [int(lowercase__ ) for c in F'''{ruleset:08}'''[:8]]

def 				__UpperCamelCase							(	lowercase__						:  list[list[int]],					lowercase__						:  list[int],					lowercase__						:  int ):






     '''simple docstring'''





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

def 				__UpperCamelCase							(	lowercase__						:  list[list[int]] ):






     '''simple docstring'''





     __lowercase					=Image.new('RGB',					(len(cells[0] ), len(lowercase__ )) )
     __lowercase					=img.load()
     # Generates image
     for w in range(img.width ):
          for h in range(img.height ):
               __lowercase					=2_55 - int(2_55 * cells[h][w] )
               __lowercase					=(color, color, color)
     return img


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





'''simple docstring'''

from __future__ import annotations

def 				__UpperCamelCase							(	lowercase__						:  list[int | float],					lowercase__						:  int,					lowercase__						:  int ):






     '''simple docstring'''





     if len(lowercase__ ) == 0:
          raise ValueError('find_min() arg is an empty sequence' )
     if (
         left >= len(lowercase__ )
         or left < -len(lowercase__ )
         or right >= len(lowercase__ )
         or right < -len(lowercase__ )
     ):
          raise IndexError('list index out of range' )
     if left == right:
          return nums[left]
     __lowercase					=(left + right) >> 1  # the middle
     __lowercase					=find_min(lowercase__,					lowercase__,					lowercase__ )  # find min in range[left, mid]
     __lowercase					=find_min(lowercase__,					mid + 1,					lowercase__ )  # find min in range[mid + 1, right]

     return left_min if left_min <= right_min else right_min


if __name__ == "__main__":
       import doctest

       doctest.testmod(verbose=True)


