12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # 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)
- 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):
- a, b = pos
- if dir == 0:
- a += inc
- else:
- b += inc
- pos = (a,b)
- if pos not in grid:
- grid[pos] = [0,0]
- grid[pos][i] = 1
- dists = []
- for k,v in grid.items():
- if v == [1,1] and k != (0,0):
- dists.append(abs(k[0]) + abs(k[1]))
- print(dists)
- print(min(dists))
-
- if __name__ == "__main__":
- main(False)
|