# Python 3.7 def main(debug): paths = {} if debug: #paths = {1: ["R75","D30","R83","U83","L12","D49","R71","U7","L72"], # 2: ["U62","R66","U55","R34","D71","R55","D58","R83"]} paths = {1: ["R98","U47","R26","D63","R33","U87","L62","D20","R33","U53","R51"], 2: ["U98","R91","D20","R16","D67","R40","U7","R15","U6","R7"]} else: with open("03in.txt","r") as file: for i in range(2): # there are only two lines paths[i+1] = file.readline().strip("\n").split(",") grid = {} for i in range(2): pos = (0,0) steps = 0 for path in paths[i+1]: d, v = path[0], int(path[1:]) if d == "U": dir = 1 inc = 1 elif d == "D": dir = 1 inc = -1 elif d == "R": dir = 0 inc = 1 else: dir = 0 inc = -1 for _ in range(v): steps += 1 a, b = pos if dir == 0: a += inc else: b += inc pos = (a,b) if pos not in grid: grid[pos] = [0,0] if grid[pos][i] == 0: grid[pos][i] = steps dists = [] for k,v in grid.items(): if v[0] != 0 and v[1] != 0 and k != (0,0): dists.append(abs(v[0]) + abs(v[1])) print(sorted(dists)) print(min(dists)) if __name__ == "__main__": main(False)