
"""simple docstring"""

class __lowerCAmelCase	:





     def __init__(			self			,  __UpperCAmelCase			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= None
      __UpperCamelCase       						= None
      __UpperCamelCase       						= graph

      self._normalize_graph(__UpperCAmelCase			,  __UpperCAmelCase   )
      __UpperCamelCase       						= len(__UpperCAmelCase   )
      __UpperCamelCase       						= None





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      if sources is int:
       __UpperCamelCase       						= [sources]
      if sinks is int:
       __UpperCamelCase       						= [sinks]

      if len(__UpperCAmelCase   ) == 0 or len(__UpperCAmelCase   ) == 0:
       return

      __UpperCamelCase       						= sources[0]
      __UpperCamelCase       						= sinks[0]

      # make fake vertex if there are more
      # than one source or sink
      if len(__UpperCAmelCase   ) > 1 or len(__UpperCAmelCase   ) > 1:
       __UpperCamelCase       						= 0
       for i in sources:
        max_input_flow += sum(self.graph[i]   )

       __UpperCamelCase       						= len(self.graph   ) + 1
       for room in self.graph:
        room.insert(0			,  0   )
       self.graph.insert(0			,  [0] * size   )
       for i in sources:
        __UpperCamelCase       						= max_input_flow
       __UpperCamelCase       						= 0

       __UpperCamelCase       						= len(self.graph   ) + 1
       for room in self.graph:
        room.append(0   )
       self.graph.append([0] * size   )
       for i in sinks:
        __UpperCamelCase       						= max_input_flow
       __UpperCamelCase       						= size - 1





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      if self.maximum_flow_algorithm is None:
       raise Exception('You need to set maximum flow algorithm before.'   )
      if self.source_index is None or self.sink_index is None:
       return 0

      self.maximum_flow_algorithm.execute()
      return self.maximum_flow_algorithm.getMaximumFlow()







     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= algorithm(self   )


class __lowerCAmelCase	:





     def __init__(			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= flow_network
      __UpperCamelCase       						= flow_network.verticesCount
      __UpperCamelCase       						= flow_network.sourceIndex
      __UpperCamelCase       						= flow_network.sinkIndex
      # it's just a reference, so you shouldn't change
      # it in your algorithms, use deep copy before doing that
      __UpperCamelCase       						= flow_network.graph
      __UpperCamelCase       						= False





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      if not self.executed:
       self._algorithm()
       __UpperCamelCase       						= True







     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      pass


class __lowerCAmelCase	(   A_    ):





     def __init__(			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      super().__init__(__UpperCAmelCase   )
      # use this to save your result
      __UpperCamelCase       						= -1







     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      if not self.executed:
       raise Exception('You should execute algorithm before using its result!'   )

      return self.maximum_flow


class __lowerCAmelCase	(   A_    ):





     def __init__(			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      super().__init__(__UpperCAmelCase   )

      __UpperCamelCase       						= [[0] * self.verticies_count for i in range(self.verticies_count   )]

      __UpperCamelCase       						= [0] * self.verticies_count
      __UpperCamelCase       						= [0] * self.verticies_count





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.verticies_count

      # push some substance to graph
      for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index]   ):
       self.preflow[self.source_index][nextvertex_index] += bandwidth
       self.preflow[nextvertex_index][self.source_index] -= bandwidth
       self.excesses[nextvertex_index] += bandwidth

      # Relabel-to-front selection rule
      __UpperCamelCase       						= [
          i
          for i in range(self.verticies_count   )
          if i != self.source_index and i != self.sink_index
      ]

      # move through list
      __UpperCamelCase       						= 0
      while i < len(__UpperCAmelCase   ):
       __UpperCamelCase       						= vertices_list[i]
       __UpperCamelCase       						= self.heights[vertex_index]
       self.process_vertex(__UpperCAmelCase   )
       if self.heights[vertex_index] > previous_height:
        # if it was relabeled, swap elements
        # and start from 0 index
        vertices_list.insert(0			,  vertices_list.pop(__UpperCAmelCase   )   )
        __UpperCamelCase       						= 0
       else:
        i += 1

      __UpperCamelCase       						= sum(self.preflow[self.source_index]   )





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      while self.excesses[vertex_index] > 0:
       for neighbour_index in range(self.verticies_count   ):
        # if it's neighbour and current vertex is higher
        if (
            self.graph[vertex_index][neighbour_index]
            - self.preflow[vertex_index][neighbour_index]
            > 0
            and self.heights[vertex_index] > self.heights[neighbour_index]
        ):
         self.push(__UpperCAmelCase			,  __UpperCAmelCase   )

       self.relabel(__UpperCAmelCase   )





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= min(
          self.excesses[from_index]			,  self.graph[from_index][to_index] - self.preflow[from_index][to_index]			,  )
      self.preflow[from_index][to_index] += preflow_delta
      self.preflow[to_index][from_index] -= preflow_delta
      self.excesses[from_index] -= preflow_delta
      self.excesses[to_index] += preflow_delta







     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= None
      for to_index in range(self.verticies_count   ):
       if (
           self.graph[vertex_index][to_index]
           - self.preflow[vertex_index][to_index]
           > 0
       ) and (min_height is None or self.heights[to_index] < min_height):
        __UpperCamelCase       						= self.heights[to_index]

      if min_height is not None:
       __UpperCamelCase       						= min_height + 1


if __name__ == "__main__":
       UpperCamelCase			:       Tuple									=				[0]
       UpperCamelCase			:       Dict									=				[3]
       # graph = [
       #     [0, 0, 4, 6, 0, 0],
       #     [0, 0, 5, 2, 0, 0],
       #     [0, 0, 0, 0, 4, 4],
       #     [0, 0, 0, 0, 6, 6],
       #     [0, 0, 0, 0, 0, 0],
       #     [0, 0, 0, 0, 0, 0],
       # ]
       UpperCamelCase			:       List[Any]									=				[[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]]

       # prepare our network
       UpperCamelCase			:       Dict									=				FlowNetwork(graph, entrances, exits)
       # set algorithm
       flow_network.set_maximum_flow_algorithm(PushRelabelExecutor)
       # and calculate
       UpperCamelCase			:       str									=				flow_network.find_maximum_flow()

       print(f'''maximum flow is {maximum_flow}''')





"""simple docstring"""

import string

import numpy






def 							A		(       snake_case   :int       ,      snake_case   :int	)   ->			int:
 return b if a == 0 else greatest_common_divisor(b % a       ,      snake_case	)

class __lowerCAmelCase	:
     lowercase		     =		string.ascii_uppercase + string.digits
     # This cipher takes alphanumerics into account
     # i.e. a total of 36 characters

     # take x and return x % len(key_string)
     lowercase		     =		numpy.vectorize(lambda __SCREAMING_SNAKE_CASE    : x % 36    )

     lowercase		     =		numpy.vectorize(__SCREAMING_SNAKE_CASE    )





     def __init__(			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.modulus(__UpperCAmelCase   )  # mod36 calc's on the encrypt key
      self.check_determinant()  # validate the determinant of the encryption key
      __UpperCamelCase       						= encrypt_key.shape[0]





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      return self.key_string.index(__UpperCAmelCase   )





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      return self.key_string[round(__UpperCAmelCase   )]





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= round(numpy.linalg.det(self.encrypt_key   )   )

      if det < 0:
       __UpperCamelCase       						= det % len(self.key_string   )

      __UpperCamelCase       						= len(self.key_string   )
      if greatest_common_divisor(__UpperCAmelCase			,  len(self.key_string   )   ) != 1:
       __UpperCamelCase       						= (
           F'determinant modular {req_l} of encryption key({det}) '
           F'is not co prime w.r.t {req_l}.\nTry another key.'
       )
       raise ValueError(__UpperCAmelCase   )





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= [char for char in text.upper() if char in self.key_string]

      __UpperCamelCase       						= chars[-1]
      while len(__UpperCAmelCase   ) % self.break_key != 0:
       chars.append(__UpperCAmelCase   )

      return "".join(__UpperCAmelCase   )





     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.process_text(text.upper()   )
      __UpperCamelCase       						= ''

      for i in range(0			,  len(__UpperCAmelCase   ) - self.break_key + 1			,  self.break_key   ):
       __UpperCamelCase       						= text[i : i + self.break_key]
       __UpperCamelCase       						= [self.replace_letters(__UpperCAmelCase   ) for char in batch]
       __UpperCamelCase       						= numpy.array([vec]   ).T
       __UpperCamelCase       						= self.modulus(self.encrypt_key.dot(__UpperCAmelCase   )   ).T.tolist()[
           0
       ]
       __UpperCamelCase       						= ''.join(
           self.replace_digits(__UpperCAmelCase   ) for num in batch_encrypted   )
       encrypted += encrypted_batch

      return encrypted





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= round(numpy.linalg.det(self.encrypt_key   )   )

      if det < 0:
       __UpperCamelCase       						= det % len(self.key_string   )
      __UpperCamelCase       						= None
      for i in range(len(self.key_string   )   ):
       if (det * i) % len(self.key_string   ) == 1:
        __UpperCamelCase       						= i
        break

      __UpperCamelCase       						= (
          det_inv
          * numpy.linalg.det(self.encrypt_key   )
          * numpy.linalg.inv(self.encrypt_key   )
      )

      return self.to_int(self.modulus(__UpperCAmelCase   )   )







     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= self.make_decrypt_key()
      __UpperCamelCase       						= self.process_text(text.upper()   )
      __UpperCamelCase       						= ''

      for i in range(0			,  len(__UpperCAmelCase   ) - self.break_key + 1			,  self.break_key   ):
       __UpperCamelCase       						= text[i : i + self.break_key]
       __UpperCamelCase       						= [self.replace_letters(__UpperCAmelCase   ) for char in batch]
       __UpperCamelCase       						= numpy.array([vec]   ).T
       __UpperCamelCase       						= self.modulus(decrypt_key.dot(__UpperCAmelCase   )   ).T.tolist()[0]
       __UpperCamelCase       						= ''.join(
           self.replace_digits(__UpperCAmelCase   ) for num in batch_decrypted   )
       decrypted += decrypted_batch

      return decrypted






def 							A		(       )   ->			None:
 __UpperCamelCase       						= int(input('Enter the order of the encryption key: '	)	)
 __UpperCamelCase       						= []

 print('Enter each row of the encryption key with space separated integers'	)
 for _ in range(snake_case	):
  __UpperCamelCase       						= [int(snake_case	) for x in input().split()]
  hill_matrix.append(snake_case	)

 __UpperCamelCase       						= HillCipher(numpy.array(snake_case	)	)

 print('Would you like to encrypt or decrypt some text? (1 or 2)'	)
 __UpperCamelCase       						= input('\n1. Encrypt\n2. Decrypt\n'	)
 if option == "1":
  __UpperCamelCase       						= input('What text would you like to encrypt?: '	)
  print('Your encrypted text is:'	)
  print(hc.encrypt(snake_case	)	)
 elif option == "2":
  __UpperCamelCase       						= input('What text would you like to decrypt?: '	)
  print('Your decrypted text is:'	)
  print(hc.decrypt(snake_case	)	)


if __name__ == "__main__":
      import doctest

      doctest.testmod()

      main()



