



UpperCAmelCase_		 =					[sum(int(c, 1_0) ** 2 for c in i.__str__()) for i in range(1_0_0_0_0_0)]

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int				)  ->     int:
   _A      =       0
   while number:
      # Increased Speed Slightly by checking every 5 digits together.
      sum_of_digits_squared += DIGITS_SQUARED[number % 100_000]
      number //= 100_000

   return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
UpperCAmelCase_		 =					[None] * 1_0_0_0_0_0_0_0
UpperCAmelCase_		 =					True
UpperCAmelCase_		 =					False

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int				)  ->     bool:

   if CHAINS[number - 1] is not None:
      return CHAINS[number - 1]  # type: ignore

   _A      =       chain(next_number(_snake_case				)				)
   _A      =       number_chain

   while number < 10_000_000:
      _A      =       number_chain
      number *= 10

   return number_chain

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:int = 10_000_000				)  ->     int:
   for i in range(1	,   _snake_case				):
      if CHAINS[i] is None:
         chain(i + 1				)

   return CHAINS[:number].count(_snake_case				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   print(f'{solution() = }')










'''simple docstring'''






from __future__ import annotations

__UpperCamelCase					    =	[
    [-1, 0],  # left
    [0, -1],  # down
    [1, 0],  # right
    [0, 1],  # up
]







def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase    ,	) ->				tuple[list[list[int]], list[list[int]]]:



			"""simple docstring"""


			__snake_case					:      List[str]							      =     [
			    [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) )
			]  # the reference grid
			__snake_case					:      Tuple							      =     1
			__snake_case					:      List[str]							      =     [
			    [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) )
			]  # the action grid

			__snake_case					:      List[str]							      =     init[0]
			__snake_case					:      str							      =     init[1]
			__snake_case					:      int							      =     0
			__snake_case					:      int							      =     g + heuristic[x][y]  # cost from starting cell to destination cell
			__snake_case					:      List[str]							      =     [[f, g, x, y]]

			__snake_case					:      Any							      =     False  # flag that is set when search is complete
			__snake_case					:      int							      =     False  # flag set if we can't find expand

			while not found and not resign:
						if len(_lowerCamelCase ) == 0:
									raise ValueError("""Algorithm is unable to find solution""" )
						else:  # to choose the least costliest action so as to move closer to the goal
									cell.sort()
									cell.reverse()
									__snake_case					:      Tuple							      =     cell.pop()
									__snake_case					:      Optional[int]							      =     next_cell[2]
									__snake_case					:      List[Any]							      =     next_cell[3]
									__snake_case					:      int							      =     next_cell[1]

									if x == goal[0] and y == goal[1]:
												__snake_case					:      Optional[Any]							      =     True
									else:
												for i in range(len(_lowerCamelCase ) ):  # to try out different valid actions
															__snake_case					:      Union[str, Any]							      =     x + DIRECTIONS[i][0]
															__snake_case					:      str							      =     y + DIRECTIONS[i][1]
															if xa >= 0 and xa < len(_lowerCamelCase ) and ya >= 0 and ya < len(grid[0] ):
																		if closed[xa][ya] == 0 and grid[xa][ya] == 0:
																					__snake_case					:      str							      =     g + cost
																					__snake_case					:      Tuple							      =     ga + heuristic[xa][ya]
																					cell.append([fa, ga, xa, ya] )
																					__snake_case					:      List[str]							      =     1
																					__snake_case					:      Optional[int]							      =     i
			__snake_case					:      List[str]							      =     []
			__snake_case					:      Optional[int]							      =     goal[0]
			__snake_case					:      List[Any]							      =     goal[1]
			invpath.append([x, y] )  # we get the reverse path from here
			while x != init[0] or y != init[1]:
						__snake_case					:      Dict							      =     x - DIRECTIONS[action[x][y]][0]
						__snake_case					:      int							      =     y - DIRECTIONS[action[x][y]][1]
						__snake_case					:      Optional[int]							      =     xa
						__snake_case					:      int							      =     ya
						invpath.append([x, y] )

			__snake_case					:      Optional[int]							      =     []
			for i in range(len(_lowerCamelCase ) ):
						path.append(invpath[len(_lowerCamelCase ) - 1 - i] )
			return path, action


if __name__ == "__main__":
	__UpperCamelCase					    =	[
	    [0, 1, 0, 0, 0, 0],
	    [0, 1, 0, 0, 0, 0],  # 0 are free path whereas 1's are obstacles
	    [0, 1, 0, 0, 0, 0],
	    [0, 1, 0, 0, 1, 0],
	    [0, 0, 0, 0, 1, 0],
	]

	__UpperCamelCase					    =	[0, 0]
	# all coordinates are given in format [y,x]
	__UpperCamelCase					    =	[len(grid) - 1, len(grid[0]) - 1]
	__UpperCamelCase					    =	1

	# the cost map which pushes the path closer to the goal
	__UpperCamelCase					    =	[[0 for row in range(len(grid[0]))] for col in range(len(grid))]
	for i in range(len(grid)):
		for j in range(len(grid[0])):
			__UpperCamelCase					    =	abs(i - goal[0]) + abs(j - goal[1])
			if grid[i][j] == 1:
				# added extra penalty in the heuristic map
				__UpperCamelCase					    =	99

	__UpperCamelCase    ,		__UpperCamelCase					    =	search(grid, init, goal, cost, heuristic)

	print("ACTION MAP")
	for i in range(len(action)):
		print(action[i])

	for i in range(len(path)):
		print(path[i])


