






from abc import ABC, abstractmethod
from typing import List, Optional



class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		def __init__(							self       )     ->							str:
							# test for the above condition
							self.test()



		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							UpperCAmelCase_   :				Tuple						=						0
							UpperCAmelCase_   :				List[Any]						=						False
							while not completed:
												if counter == 1:
																	self.reset()
												UpperCAmelCase_   :				Optional[int]						=						self.advance()
												if not self.does_advance(_UpperCamelCase       ):
																	raise Exception(
																	    'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'       )

												UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_   :				Optional[Any]						=						self.update(_UpperCamelCase       )
												counter += 1

												if counter > 1_0_0_0_0:
																	raise Exception('update() does not fulfill the constraint.'       )

							if self.remaining() != 0:
												raise Exception('Custom Constraint is not defined correctly.'       )



		@abstractmethod
		def  __UpperCAmelCase      (							self       )     ->							int:
							raise NotImplementedError(
							    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."       )



		@abstractmethod
		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Optional[Any]:
							raise NotImplementedError(
							    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."       )



		@abstractmethod
		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Union[str, Any]:
							raise NotImplementedError(
							    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."       )



		@abstractmethod
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							raise NotImplementedError(
							    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."       )



		@abstractmethod
		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							raise NotImplementedError(
							    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."       )



		@abstractmethod
		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase=False       )     ->							List[Any]:
							raise NotImplementedError(
							    f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."       )





class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		def __init__(							self    ,		_UpperCamelCase       )     ->							Union[str, Any]:
							super(_UpperCamelCase    ,		self       ).__init__()

							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) or len(_UpperCamelCase       ) == 0:
												raise ValueError(f"`token_ids` has to be a non-empty list, but is {token_ids}."       )
							if any((not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) or token_id < 0) for token_id in token_ids       ):
												raise ValueError(f"Each list in `token_ids` has to be a list of positive integers, but is {token_ids}."       )

							UpperCAmelCase_   :				List[Any]						=						token_ids

							UpperCAmelCase_   :				Dict						=						len(self.token_ids       )
							UpperCAmelCase_   :				str						=						-1  # the index of the currently fulfilled step
							UpperCAmelCase_   :				int						=						False



		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							if self.completed:
												return None
							return self.token_ids[self.fulfilled_idx + 1]



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Optional[int]:
							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(_UpperCamelCase       )}"       )

							if self.completed:
												return False

							return token_id == self.token_ids[self.fulfilled_idx + 1]



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Optional[int]:
							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(_UpperCamelCase       )}"       )

							UpperCAmelCase_   :				Union[str, Any]						=						False
							UpperCAmelCase_   :				Tuple						=						False
							UpperCAmelCase_   :				Any						=						False

							if self.does_advance(_UpperCamelCase       ):
												self.fulfilled_idx += 1
												UpperCAmelCase_   :				Any						=						True
												if self.fulfilled_idx == (self.seqlen - 1):
																	UpperCAmelCase_   :				Optional[Any]						=						True
												UpperCAmelCase_   :				List[str]						=						completed
							else:
												# failed to make progress.
												UpperCAmelCase_   :				Optional[Any]						=						True
												self.reset()
							return stepped, completed, reset



		def  __UpperCAmelCase      (							self       )     ->							Dict:
							UpperCAmelCase_   :				str						=						False
							UpperCAmelCase_   :				Any						=						0



		def  __UpperCAmelCase      (							self       )     ->							Any:
							return self.seqlen - (self.fulfilled_idx + 1)



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase=False       )     ->							Any:
							UpperCAmelCase_   :				str						=						PhrasalConstraint(self.token_ids       )

							if stateful:
												UpperCAmelCase_   :				Tuple						=						self.seqlen
												UpperCAmelCase_   :				Any						=						self.fulfilled_idx
												UpperCAmelCase_   :				List[str]						=						self.completed

							return new_constraint





class   lowerCamelCase				:







		'''simple docstring'''



		def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase=True       )     ->							Union[str, Any]:
							UpperCAmelCase_   :				List[Any]						=						max([len(_UpperCamelCase       ) for one in nested_token_ids]       )

							UpperCAmelCase_   :				Dict						=						{}
							for token_ids in nested_token_ids:
												UpperCAmelCase_   :				Tuple						=						root
												for tidx, token_id in enumerate(_UpperCamelCase       ):
																	if token_id not in level:
																						UpperCAmelCase_   :				str						=						{}

																	UpperCAmelCase_   :				Optional[Any]						=						level[token_id]

							if no_subsets and self.has_subsets(_UpperCamelCase    ,		_UpperCamelCase       ):
												raise ValueError(
												    'Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'
												    f" {nested_token_ids}."       )

							UpperCAmelCase_   :				str						=						root



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Tuple:
							UpperCAmelCase_   :				Dict						=						self.trie

							for current_token in current_seq:
												UpperCAmelCase_   :				int						=						start[current_token]

							UpperCAmelCase_   :				List[Any]						=						list(start.keys()       )

							return next_tokens



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							List[Any]:
							UpperCAmelCase_   :				Optional[int]						=						self.next_tokens(_UpperCamelCase       )

							return len(_UpperCamelCase       ) == 0



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							int:
							UpperCAmelCase_   :				str						=						list(root.values()       )
							if len(_UpperCamelCase       ) == 0:
												return 1
							else:
												return sum([self.count_leaves(_UpperCamelCase       ) for nn in next_nodes]       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							List[Any]:
							UpperCAmelCase_   :				Tuple						=						self.count_leaves(_UpperCamelCase       )
							return len(_UpperCamelCase       ) != leaf_count





class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		def __init__(							self    ,		_UpperCamelCase       )     ->							List[Any]:
							super(_UpperCamelCase    ,		self       ).__init__()

							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) or len(_UpperCamelCase       ) == 0:
												raise ValueError(f"`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."       )
							if any(not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) for token_ids in nested_token_ids       ):
												raise ValueError(f"`nested_token_ids` has to be a list of lists, but is {nested_token_ids}."       )
							if any(
							    any((not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ) or token_id < 0) for token_id in token_ids       )
							    for token_ids in nested_token_ids       ):
												raise ValueError(
												    f"Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}."       )

							UpperCAmelCase_   :				Optional[Any]						=						DisjunctiveTrie(_UpperCamelCase       )
							UpperCAmelCase_   :				Any						=						nested_token_ids

							UpperCAmelCase_   :				List[Any]						=						self.trie.max_height
							UpperCAmelCase_   :				str						=						[]
							UpperCAmelCase_   :				Tuple						=						False



		def  __UpperCAmelCase      (							self       )     ->							int:
							UpperCAmelCase_   :				Union[str, Any]						=						self.trie.next_tokens(self.current_seq       )

							if len(_UpperCamelCase       ) == 0:
												return None
							else:
												return token_list



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Union[str, Any]:
							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(_UpperCamelCase       )}"       )

							UpperCAmelCase_   :				Dict						=						self.trie.next_tokens(self.current_seq       )

							return token_id in next_tokens



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Union[str, Any]:
							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(_UpperCamelCase       )}"       )

							UpperCAmelCase_   :				str						=						False
							UpperCAmelCase_   :				Union[str, Any]						=						False
							UpperCAmelCase_   :				int						=						False

							if self.does_advance(_UpperCamelCase       ):
												self.current_seq.append(_UpperCamelCase       )
												UpperCAmelCase_   :				Any						=						True
							else:
												UpperCAmelCase_   :				List[str]						=						True
												self.reset()

							UpperCAmelCase_   :				Optional[Any]						=						self.trie.reached_leaf(self.current_seq       )
							UpperCAmelCase_   :				Tuple						=						completed

							return stepped, completed, reset



		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							UpperCAmelCase_   :				Optional[Any]						=						False
							UpperCAmelCase_   :				Optional[Any]						=						[]



		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							if self.completed:
												# since this can be completed without reaching max height
												return 0
							else:
												return self.seqlen - len(self.current_seq       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase=False       )     ->							List[Any]:
							UpperCAmelCase_   :				int						=						DisjunctiveConstraint(self.token_ids       )

							if stateful:
												UpperCAmelCase_   :				int						=						self.seqlen
												UpperCAmelCase_   :				str						=						self.current_seq
												UpperCAmelCase_   :				List[Any]						=						self.completed

							return new_constraint





class   lowerCamelCase				:







		'''simple docstring'''



		def __init__(							self    ,		_UpperCamelCase       )     ->							int:
							UpperCAmelCase_   :				Tuple						=						constraints

							# max # of steps required to fulfill a given constraint
							UpperCAmelCase_   :				Any						=						max([c.seqlen for c in constraints]       )
							UpperCAmelCase_   :				Tuple						=						len(_UpperCamelCase       )
							UpperCAmelCase_   :				List[Any]						=						False

							self.init_state()



		def  __UpperCAmelCase      (							self       )     ->							int:
							UpperCAmelCase_   :				Dict						=						[]
							UpperCAmelCase_   :				List[str]						=						None
							UpperCAmelCase_   :				Any						=						[constraint.copy(stateful=_UpperCamelCase       ) for constraint in self.constraints]



		def  __UpperCAmelCase      (							self       )     ->							int:
							UpperCAmelCase_   :				List[Any]						=						0
							if self.inprogress_constraint:
												# extra points for having a constraint mid-fulfilled
												add += self.max_seqlen - self.inprogress_constraint.remaining()

							return (len(self.complete_constraints       ) * self.max_seqlen) + add



		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							UpperCAmelCase_   :				List[Any]						=						[]
							if self.inprogress_constraint is None:
												for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
																	UpperCAmelCase_   :				Optional[Any]						=						constraint.advance()
																	if isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
																						token_list.append(_UpperCamelCase       )
																	elif isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
																						token_list.extend(_UpperCamelCase       )
							else:
												UpperCAmelCase_   :				Union[str, Any]						=						self.inprogress_constraint.advance()
												if isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
																	token_list.append(_UpperCamelCase       )
												elif isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
																	token_list.extend(_UpperCamelCase       )

							if len(_UpperCamelCase       ) == 0:
												return None
							else:
												return token_list



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Any:
							self.init_state()

							if token_ids is not None:
												for token in token_ids:
																	# completes or steps **one** constraint
																	UpperCAmelCase_	,  UpperCAmelCase_   :				Optional[Any]						=						self.add(_UpperCamelCase       )

																	# the entire list of constraints are fulfilled
																	if self.completed:
																						break



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Any:
							if not isinstance(_UpperCamelCase    ,		_UpperCamelCase       ):
												raise ValueError(f"`token_id` should be an `int`, but is `{token_id}`."       )

							UpperCAmelCase_	,  UpperCAmelCase_   :				List[str]						=						False, False

							if self.completed:
												UpperCAmelCase_   :				Tuple						=						True
												UpperCAmelCase_   :				Optional[Any]						=						False
												return complete, stepped

							if self.inprogress_constraint is not None:
												# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
												# job, simply update the state

												UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_   :				Optional[int]						=						self.inprogress_constraint.update(_UpperCamelCase       )
												if reset:
																	# 1. If the next token breaks the progress, then we must restart.
																	#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

																	#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
																	#     constraint, not the full list of constraints.

																	self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_UpperCamelCase       )       )
																	UpperCAmelCase_   :				Tuple						=						None

												if complete:
																	# 2. If the next token completes the constraint, move it to completed list, set
																	#     inprogress to None. If there are no pending constraints either, then this full list of constraints
																	#     is complete.

																	self.complete_constraints.append(self.inprogress_constraint       )
																	UpperCAmelCase_   :				Dict						=						None

																	if len(self.pending_constraints       ) == 0:
																						# we're done!
																						UpperCAmelCase_   :				str						=						True

							else:
												# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
												# of constraints?

												for cidx, pending_constraint in enumerate(self.pending_constraints       ):
																	if pending_constraint.does_advance(_UpperCamelCase       ):
																						UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_   :				str						=						pending_constraint.update(_UpperCamelCase       )

																						if not stepped:
																											raise Exception(
																											    '`constraint.update(token_id)` is not yielding incremental progress, '
																											    'even though `constraint.does_advance(token_id)` is true.'       )

																						if complete:
																											self.complete_constraints.append(_UpperCamelCase       )
																											UpperCAmelCase_   :				Optional[Any]						=						None

																						if not complete and stepped:
																											UpperCAmelCase_   :				str						=						pending_constraint

																						if complete or stepped:
																											# If we made any progress at all, then it's at least not a "pending constraint".

																											UpperCAmelCase_   :				Optional[int]						=						(
																											    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																											)

																											if len(self.pending_constraints       ) == 0 and self.inprogress_constraint is None:
																																# If there's no longer any pending after this and no inprogress either, then we must be
																																# complete.

																																UpperCAmelCase_   :				str						=						True

																											break  # prevent accidentally stepping through multiple constraints with just one token.

							return complete, stepped



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase=True       )     ->							Dict:
							UpperCAmelCase_   :				Union[str, Any]						=						ConstraintListState(self.constraints       )  # we actually never though self.constraints objects
							# throughout this process. So it's at initialization state.

							if stateful:
												UpperCAmelCase_   :				List[Any]						=						[
												    constraint.copy(stateful=_UpperCamelCase       ) for constraint in self.complete_constraints
												]
												if self.inprogress_constraint is not None:
																	UpperCAmelCase_   :				List[Any]						=						self.inprogress_constraint.copy(stateful=_UpperCamelCase       )
												UpperCAmelCase_   :				Union[str, Any]						=						[constraint.copy() for constraint in self.pending_constraints]

							return new_state














def 					lowercase__				(					__snake_case   :							Tuple					,	__snake_case   :							List[Any]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Any						=						''
					for i in table:
										res += inp[i - 1]
					return res




def 					lowercase__				(					__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					return data[1:] + data[0]




def 					lowercase__				(					__snake_case   :							Union[str, Any]					,	__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Any						=						''
					for i in range(len(__snake_case  )  ):
										if a[i] == b[i]:
															res += "0"
										else:
															res += "1"
					return res




def 					lowercase__				(					__snake_case   :							Dict					,	__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Optional[Any]						=						int('0b' + data[0] + data[-1]					,	2  )
					UpperCAmelCase_   :				Any						=						int('0b' + data[1:3]					,	2  )
					return bin(s[row][col]  )[2:]




def 					lowercase__				(					__snake_case   :							Tuple					,	__snake_case   :							List[Any]					,	__snake_case   :							List[Any]					,	__snake_case   :							Optional[int]					,	__snake_case   :							str  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Union[str, Any]						=						message[:4]
					UpperCAmelCase_   :				Optional[int]						=						message[4:]
					UpperCAmelCase_   :				Union[str, Any]						=						apply_table(__snake_case					,	__snake_case  )
					UpperCAmelCase_   :				Union[str, Any]						=						xor(__snake_case					,	__snake_case  )
					UpperCAmelCase_   :				Dict						=						apply_sbox(__snake_case					,	temp[:4]  )  # noqa: E741
					UpperCAmelCase_   :				Any						=						apply_sbox(__snake_case					,	temp[4:]  )
					UpperCAmelCase_   :				str						=						'0' * (2 - len(__snake_case  )) + l  # noqa: E741
					UpperCAmelCase_   :				int						=						'0' * (2 - len(__snake_case  )) + r
					UpperCAmelCase_   :				int						=						apply_table(l + r					,	__snake_case  )
					UpperCAmelCase_   :				int						=						xor(__snake_case					,	__snake_case  )
					return temp + right


if __name__ == "__main__":
		__UpperCAmelCase     							=						input('Enter 10 bit key: ')
		__UpperCAmelCase     							=						input('Enter 8 bit message: ')

		__UpperCAmelCase     							=						[6, 3, 7, 4, 8, 5, 10, 9]
		__UpperCAmelCase     							=						[3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
		__UpperCAmelCase     							=						[2, 4, 3, 1]
		__UpperCAmelCase     							=						[2, 6, 3, 1, 4, 8, 5, 7]
		__UpperCAmelCase     							=						[4, 1, 3, 5, 7, 2, 8, 6]
		__UpperCAmelCase     							=						[4, 1, 2, 3, 2, 3, 4, 1]
		__UpperCAmelCase     							=						[[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
		__UpperCAmelCase     							=						[[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

		# key generation
		__UpperCAmelCase     							=						apply_table(key, paa_table)
		__UpperCAmelCase     							=						temp[:5]
		__UpperCAmelCase     							=						temp[5:]
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						apply_table(left + right, pa_table)
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						apply_table(left + right, pa_table)

		# encryption
		__UpperCAmelCase     							=						apply_table(message, IP)
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						temp[4:] + temp[:4]
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						apply_table(temp, IP_inv)
		print('Cipher text is:', CT)

		# decryption
		__UpperCAmelCase     							=						apply_table(CT, IP)
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						temp[4:] + temp[:4]
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						apply_table(temp, IP_inv)
		print('Plain text after decypting is:', PT)






