l = ["....#.....", ".........#", "..........", "..#.......", ".......#..", "..........", ".#..^.....", "........#.", "#.........", "......#..."] global up, down, left, right up,down,left,right = "^","v","<",">" dir = [up,down,left,right] act_dir = None global obstacles obstacles = [] pose = None # global path path = [] global h,w h,w = len(l),len(l[0]) def move_up(pose, path): print(path) out_of_map = False while pose[0] - 1 not in obstacles: #or pose[0] != 0: print(pose, path,pose in path) if pose not in path: path.append(pose) pose[0] = pose[0] - 1 print(pose) if pose[0] == 0: out_of_map = True break print(path) act_dir = right return pose, act_dir, out_of_map, path def move_down(pose): out_of_map = False while pose[0] + 1 not in obstacles or pose[0] == h-1: pose[0] = pose[0] + 1 print(pose in path) if pose not in path: path.append(pose) if pose[0] == h-1: out_of_map = True act_dir = left return pose, act_dir, out_of_map def move_left(pose): out_of_map = False while pose[1] - 1 not in obstacles or pose[1] == 0: pose[1] = pose[1] - 1 if pose not in path: path.append(pose) if pose[1] == 0: out_of_map = True act_dir = up return pose, act_dir, out_of_map def move_right(pose): out_of_map = False while pose[1] + 1 not in obstacles or pose[1] == w - 1: pose[1] = pose[1] + 1 if pose not in path: path.append(pose) if pose[1] == w - 1: out_of_map = True act_dir = down return pose, act_dir, out_of_map for i in range(len(l)): for j in range(len(l[0])): if l[i][j] in dir: act_dir = l[i][j] pose = [i,j] path.append(pose) elif l[i][j] == "#": obstacles.append([i,j]) print(obstacles) print(path) out_of_map = False while not out_of_map: if act_dir == up: pose, act_dir, out_of_map, path = move_up(pose,path) elif act_dir == down: pose, act_dir, out_of_map = move_down(pose) elif act_dir == left: pose, act_dir, out_of_map = move_left(pose) elif act_dir == right: pose, act_dir, out_of_map = move_right(pose) print(len(path))