import subprocess
def dfs(path, n, x_location, y_location, count, try_direction, visited_paths, coordinates):
if n < count:
for direction in try_direction:
process = subprocess.Popen("./mapmap", shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
next_path = path + direction
process.stdin.write((next_path + '\n').encode())
process.stdin.flush()
output, _ = process.communicate()
if b'wrong\n' not in output:
new_x_location, new_y_location = x_location, y_location
if direction == 'd':
new_x_location += 1
elif direction == 'a':
new_x_location -= 1
elif direction == 'w':
new_y_location -= 1
elif direction == 's':
new_y_location += 1
if (new_x_location, new_y_location) not in coordinates:
print(path)
coordinates[(new_x_location, new_y_location)] = next_path
dfs(next_path, len(next_path), new_x_location, new_y_location, count, try_direction, visited_paths, coordinates)
elif n == count:
visited_paths.append(path)
coordinates[(x_location, y_location)] = path
if __name__ == '__main__':
start_path = ''
try_direction = ['w', 'd', 's', 'a']
count = 140
visited_paths = []
coordinates = {}
x_location = 0
y_location = 0
coordinates[(x_location, y_location)] = start_path
print("\nVisit paths:")
dfs(start_path, len(start_path), x_location, y_location, count, try_direction, visited_paths, coordinates)
print("\nFinal paths:")
for path in visited_paths:
process = subprocess.Popen("./mapmap", shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
process.stdin.write((path + '\n').encode())
process.stdin.flush()
output, _ = process.communicate()
if b'flag{md5(your input)}\n' in output:
print(path)