Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. import re
  2.  
  3. with open('input.txt') as file :
  4.     moves = re.findall('\w\d+', file.read())
  5.  
  6. AMoves = moves[:len(moves)//2]
  7. BMoves = moves[len(moves)//2 :]
  8.  
  9. print(len(AMoves), len(BMoves))
  10.  
  11. def update(move) :
  12.     if move[0] == 'R' :
  13.         return [int(move[1:]),0]
  14.     if move[0] == 'U' :
  15.         return [0, int(move[1:])]
  16.     if move[0] == 'D' :
  17.         return [0, -1*int(move[1:])]
  18.     if move[0] == 'L' :
  19.         return [-1*int(move[1:]),0]
  20.  
  21.  
  22. def evolve(AMoves = AMoves, BMoves = BMoves) :
  23.     apos , bpos = [0,0] , [0,0]
  24.     atraj, btraj = [apos],[bpos]
  25.     for i in range(len(AMoves)) :
  26.         apos[0] += update(AMoves[i])[0]
  27.         bpos[0] += update(BMoves[i])[0]
  28.         apos[1] += update(AMoves[i])[1]
  29.         bpos[1] += update(BMoves[i])[1]
  30.  
  31.         atraj.append(apos)
  32.         btraj.append(bpos)
  33.         print(atraj[-1], btraj[-1])
  34.  
  35.         if apos in btraj :
  36.             print("Intersect")
  37.             try :
  38.                 mindist = min(mindist, abs(apos[0]) + abs(apos[1]))
  39.             except:
  40.                 mindist = abs(apos[0]) + abs(apos[1])
  41.  
  42.         if bpos in atraj :
  43.             print("Intersect")
  44.             try :
  45.                 mindist = min(mindist, abs(apos[0]) + abs(apos[1]))
  46.             except:
  47.                 mindist = abs(apos[0]) + abs(apos[1])
  48.  
  49.     print(mindist)
  50.  
  51. evolve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement