Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- def trace(l, direction, steps):
- if steps == 0:
- return l
- else:
- if direction == 'U':
- while steps > 0:
- new_coord = (l[-1][0], l[-1][1] + 1)
- l.append(new_coord)
- steps -= 1
- return l
- elif direction == 'D':
- while steps > 0:
- new_coord = (l[-1][0], l[-1][1] - 1)
- l.append(new_coord)
- steps -= 1
- return l
- elif direction == 'R':
- while steps > 0:
- new_coord = (l[-1][0] + 1, l[-1][1])
- l.append(new_coord)
- steps -= 1
- return l
- elif direction == 'L':
- while steps > 0:
- new_coord = (l[-1][0] - 1, l[-1][1])
- l.append(new_coord)
- steps -= 1
- return l
- 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 = [(0, 0)]
- for instruction in instructions1:
- coords1 = trace(coords1, instruction[0], int(instruction[1:]))
- coords2 = [(0, 0)]
- for instruction in instructions2:
- coords2 = trace(coords2, instruction[0], int(instruction[1:]))
- crossings = set(coords1) & set(coords2)
- print(crossings)
- for x in crossings:
- print(x[0] + x[1])
- plt.scatter(*zip(*crossings))
- plt.plot(*zip(*coords1), 'r-')
- plt.plot(*zip(*coords2), 'b-')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement