Skip to content

Instantly share code, notes, and snippets.

@ishank-dev
Last active December 30, 2020 14:40
Show Gist options
  • Save ishank-dev/320bed6644e4622a3665a9d7df3a3654 to your computer and use it in GitHub Desktop.
Save ishank-dev/320bed6644e4622a3665a9d7df3a3654 to your computer and use it in GitHub Desktop.
'''
Logic is to find the end coordinates and start coordinates
start = [x1,y1] end = [x2,y2]
shortestDistance = abs(x2-x1) + abs(y2-y1)
Wrong moves = input_string_length - shortestDistance
'''
'''
How to run the code, run the python file and then give input_path as the string as shown below
python solve.py
TTTRRTTRRDRDDRRTTT
'''
input_path = input()
mapper = {
'T': 1,
'D': -1,
'R': 1,
'L': -1
}
# inp = TTTRRTTRRDRDDRRTTT
end_coordinates = [0, 0]
def find_end_coordinates(end_coordinates):
for i in input_path:
if i == 'T':
end_coordinates[1] += 1
elif i == 'D':
end_coordinates[1] -= 1
elif i == 'R':
end_coordinates[0] += 1
elif i == 'L':
end_coordinates[0] -= 1
return end_coordinates
end_coordinates = find_end_coordinates(end_coordinates)
start_coordinates = [0, 0]
x_distance = end_coordinates[0]-start_coordinates[0]
y_distance = end_coordinates[1]-start_coordinates[1]
length_of_shortest_path = abs(x_distance) + abs(y_distance)
extra_moves = len(input_path) - length_of_shortest_path
print('Extra moves taken are ', extra_moves)
print('Correct optimum path is:')
if x_distance < 0:
print('L'*abs(x_distance), end='')
else:
print('R'*x_distance, end='')
if y_distance < 0:
print('D'*abs(y_distance))
else:
print('T'*y_distance)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment