Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. wire1 = 'R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51'
  2. wire2 = 'U98,R91,D20,R16,D67,R40,U7,R15,U6,R7'
  3.  
  4.  
  5. paths = map(lambda p: [(a[0], int(a[1:])) for a in p.split(',')], (wire1, wire2))
  6. grids = [set(), set()]
  7.  
  8. for i, path in enumerate(paths):
  9.     o = (0, 0)
  10.     for dir, steps in path:
  11.         dx, dy = {'U': (0, 1), 'D': (0, -1), 'R': (1, 0), 'L': (-1, 0)}[dir]
  12.         grids[i] |= set((o[0] + dx * s, o[1] + dy * s) for s in range(1, steps + 1))
  13.         o = o[0] + dx * steps, o[1] + dy * steps
  14.  
  15. print(min([sum(map(abs, c)) for c in grids[0] & grids[1]]))  # first question
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement