Advertisement
dbarrera

Advent Of Code Day 3 Part 1

Dec 3rd, 2019
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3.  
  4. def move(l, coord, instruction):
  5.     if len(instruction) == 0:
  6.         return l
  7.     movement = instruction[0]
  8.     if movement[0] == 'U':
  9.         l.append((coord[0], coord[1] + int(movement[1:])))
  10.         return move(l, (coord[0], coord[1] + int(movement[1:])), instruction[1:])
  11.     elif movement[0] == 'D':
  12.         l.append((coord[0], coord[1] - int(movement[1:])))
  13.         return move(l, (coord[0], coord[1] - int(movement[1:])), instruction[1:])
  14.     elif movement[0] == 'R':
  15.         l.append((coord[0] + int(movement[1:]), coord[1]))
  16.         return move(l, (coord[0] + int(movement[1:]), coord[1]), instruction[1:])
  17.     elif movement[0] == 'L':
  18.         l.append((coord[0] - int(movement[1:]), coord[1]))
  19.         return move(l, (coord[0] - int(movement[1:]), coord[1]), instruction[1:])
  20.  
  21.  
  22. f = open('input.txt', 'r')
  23. instructions1, instructions2 = f.readlines()
  24. f.close()
  25. instructions1 = instructions1.split(',')
  26. instructions2 = instructions2.split(',')
  27.  
  28. # test case 1
  29. # instructions1 = ['R75', 'D30', 'R83', 'U83', 'L12', 'D49', 'R71', 'U7', 'L72']
  30. # instructions2 = ['U62', 'R66', 'U55', 'R34', 'D71', 'R55', 'D58', 'R83']
  31.  
  32. # test case 2
  33. # instructions1 = ['R98', 'U47', 'R26', 'D63', 'R33', 'U87', 'L62', 'D20', 'R33', 'U53', 'R51']
  34. # instructions2 = ['U98', 'R91', 'D20', 'R16', 'D67', 'R40', 'U7', 'R15', 'U6', 'R7']
  35.  
  36. coords1 = move([(0, 0)], (0, 0), instructions1)
  37. coords2 = move([(0, 0)], (0, 0), instructions2)
  38.  
  39. plt.scatter(*zip(*coords1))
  40. plt.scatter(*zip(*coords2))
  41. plt.plot(*zip(*coords1), 'r-')
  42. plt.plot(*zip(*coords2), 'b-')
  43. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement