




"""simple docstring"""







import string

import numpy





def 							lowercase					(snake_case__   : int      ,       snake_case__   : int )     ->  int:


				'''simple docstring'''






				return b if a == 0 else greatest_common_divisor(b % a      ,       snake_case__ )






class       SCREAMING_SNAKE_CASE__					:
						_a    						=						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)
						_a    						=						numpy.vectorize(lambda _a					: x % 36					)

						_a    						=						numpy.vectorize(_a					)


						def __init__(							self				:			Optional[int]						,						lowerCAmelCase				:			numpy.ndarray					):
										lowerCAmelCase  =				self.modulus(lowerCAmelCase					)  # mod36 calc's on the encrypt key
										self.check_determinant()  # validate the determinant of the encryption key
										lowerCAmelCase  =				encrypt_key.shape[0]


						def   __lowercase (							self				:			Optional[Any]						,						lowerCAmelCase				:			str					):
										return self.key_string.index(lowerCAmelCase					)


						def   __lowercase (							self				:			Optional[int]						,						lowerCAmelCase				:			int					):
										return self.key_string[round(lowerCAmelCase					)]


						def   __lowercase (							self				:			Tuple					):
										lowerCAmelCase  =				round(numpy.linalg.det(self.encrypt_key					)					)

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

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


						def   __lowercase (							self				:			List[str]						,						lowerCAmelCase				:			str					):
										lowerCAmelCase  =				[char for char in text.upper() if char in self.key_string]

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

										return "".join(lowerCAmelCase					)


						def   __lowercase (							self				:			Optional[Any]						,						lowerCAmelCase				:			str					):
										lowerCAmelCase  =				self.process_text(text.upper()					)
										lowerCAmelCase  =				""""""

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

										return encrypted


						def   __lowercase (							self				:			Union[str, Any]					):
										lowerCAmelCase  =				round(numpy.linalg.det(self.encrypt_key					)					)

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

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

										return self.to_int(self.modulus(lowerCAmelCase					)					)




						def   __lowercase (							self				:			int						,						lowerCAmelCase				:			str					):
										lowerCAmelCase  =				self.make_decrypt_key()
										lowerCAmelCase  =				self.process_text(text.upper()					)
										lowerCAmelCase  =				""""""

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

										return decrypted





def 							lowercase					()     ->  None:


				'''simple docstring'''






				lowerCAmelCase  =				int(input("""Enter the order of the encryption key: """ ) )
				lowerCAmelCase  =				[]

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

				lowerCAmelCase  =				HillCipher(numpy.array(snake_case__ ) )

				print("""Would you like to encrypt or decrypt some text? (1 or 2)""" )
				lowerCAmelCase  =				input("""\n1. Encrypt\n2. Decrypt\n""" )
				if option == "1":
								lowerCAmelCase  =				input("""What text would you like to encrypt?: """ )
								print("""Your encrypted text is:""" )
								print(hc.encrypt(snake_case__ ) )
				elif option == "2":
								lowerCAmelCase  =				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()











"""simple docstring"""







from queue import PriorityQueue
from typing import Any

import numpy as np





def 							lowercase					(snake_case__   : dict      ,       snake_case__   : str      ,       snake_case__   : set      ,       snake_case__   : set      ,       snake_case__   : dict      ,       snake_case__   : dict      ,       snake_case__   : PriorityQueue      ,       snake_case__   : dict      ,       snake_case__   : float | int      ,       )     ->  float | int:


				'''simple docstring'''






				for nxt, d in graph[v]:
								if nxt in visited_forward:
												continue
								lowerCAmelCase  =				cst_fwd.get(snake_case__      ,       np.inf )
								lowerCAmelCase  =				cst_fwd[v] + d
								if new_cost_f < old_cost_f:
												queue.put((new_cost_f, nxt) )
												lowerCAmelCase  =				new_cost_f
												lowerCAmelCase  =				v
								if nxt in visited_backward:
												if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
																lowerCAmelCase  =				cst_fwd[v] + d + cst_bwd[nxt]
				return shortest_distance





def 							lowercase					(snake_case__   : str      ,       snake_case__   : str      ,       snake_case__   : dict      ,       snake_case__   : dict )     ->  int:


				'''simple docstring'''






				lowerCAmelCase  =				-1

				lowerCAmelCase  =				set()
				lowerCAmelCase  =				set()
				lowerCAmelCase  =				{source: 0}
				lowerCAmelCase  =				{destination: 0}
				lowerCAmelCase  =				{source: None}
				lowerCAmelCase  =				{destination: None}
				lowerCAmelCase  =				PriorityQueue()
				lowerCAmelCase  =				PriorityQueue()

				lowerCAmelCase  =				np.inf

				queue_forward.put((0, source) )
				queue_backward.put((0, destination) )

				if source == destination:
								return 0

				while not queue_forward.empty() and not queue_backward.empty():
								lowerCAmelCase ,						lowerCAmelCase  =				queue_forward.get()
								visited_forward.add(snake_case__ )

								lowerCAmelCase ,						lowerCAmelCase  =				queue_backward.get()
								visited_backward.add(snake_case__ )

								lowerCAmelCase  =				pass_and_relaxation(
								    snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       )

								lowerCAmelCase  =				pass_and_relaxation(
								    snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       snake_case__      ,       )

								if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
												break

				if shortest_distance != np.inf:
								lowerCAmelCase  =				shortest_distance
				return shortest_path_distance


a      =							{
    'B': [['C', 1]],
    'C': [['D', 1]],
    'D': [['F', 1]],
    'E': [['B', 1], ['G', 2]],
    'F': [],
    'G': [['F', 1]],
}
a      =							{
    'B': [['E', 1]],
    'C': [['B', 1]],
    'D': [['C', 1]],
    'F': [['D', 1], ['G', 1]],
    'E': [[None, np.inf]],
    'G': [['E', 2]],
}

if __name__ == "__main__":
		import doctest

		doctest.testmod()





