Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- def move(l, coord, instruction):
- if len(instruction) == 0:
- return l
- movement = instruction[0]
- if movement[0] == 'U':
- l.append((coord[0], coord[1] + int(movement[1:])))
- return move(l, (coord[0], coord[1] + int(movement[1:])), instruction[1:])
- elif movement[0] == 'D':
- l.append((coord[0], coord[1] - int(movement[1:])))
- return move(l, (coord[0], coord[1] - int(movement[1:])), instruction[1:])
- elif movement[0] == 'R':
- l.append((coord[0] + int(movement[1:]), coord[1]))
- return move(l, (coord[0] + int(movement[1:]), coord[1]), instruction[1:])
- elif movement[0] == 'L':
- l.append((coord[0] - int(movement[1:]), coord[1]))
- return move(l, (coord[0] - int(movement[1:]), coord[1]), instruction[1:])
- f = open('input.txt', 'r')
- instructions1, instructions2 = f.readlines()
- f.close()
- instructions1 = instructions1.split(',')
- instructions2 = instructions2.split(',')
- # test case 1
- # instructions1 = ['R75', 'D30', 'R83', 'U83', 'L12', 'D49', 'R71', 'U7', 'L72']
- # instructions2 = ['U62', 'R66', 'U55', 'R34', 'D71', 'R55', 'D58', 'R83']
- # test case 2
- # instructions1 = ['R98', 'U47', 'R26', 'D63', 'R33', 'U87', 'L62', 'D20', 'R33', 'U53', 'R51']
- # instructions2 = ['U98', 'R91', 'D20', 'R16', 'D67', 'R40', 'U7', 'R15', 'U6', 'R7']
- coords1 = move([(0, 0)], (0, 0), instructions1)
- coords2 = move([(0, 0)], (0, 0), instructions2)
- plt.scatter(*zip(*coords1))
- plt.scatter(*zip(*coords2))
- plt.plot(*zip(*coords1), 'r-')
- plt.plot(*zip(*coords2), 'b-')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement