Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def path_to_coordinates(wire_path):
- wire_coordinates = []
- direction_to_sign = {'U': 1, 'D': -1, 'L': -1, 'R': 1}
- x, y = 0, 0
- steps = 0
- for move in wire_path:
- direction = move[0]
- magnitude = int(move[1:])
- sign = direction_to_sign[direction]
- if direction == 'L' or direction == 'R':
- x_change, y_change = sign, 0
- elif direction == 'U' or direction == 'D':
- x_change, y_change = 0, sign
- steps += magnitude
- for i in range(magnitude):
- x += x_change
- y += y_change
- wire_coordinates.append((x, y))
- return wire_coordinates, list(range(1, steps + 1))
- def manhattan_distance(coord):
- return abs(coord[0]) + abs(coord[1])
- def combined_steps(coord):
- step_count_1 = min([packed[1] for packed in zipped_1 if coord == packed[0]])
- step_count_2 = min([packed[1] for packed in zipped_2 if coord == packed[0]])
- return step_count_1 + step_count_2
- with open('day3.txt', 'r') as f:
- wire_path_1, wire_path_2 = map(lambda line: line.split(','), f.read().split())
- wire_coordinates_1, steps_1 = path_to_coordinates(wire_path_1)
- wire_coordinates_2, steps_2 = path_to_coordinates(wire_path_2)
- zipped_1 = list(list(zip(wire_coordinates_1, steps_1)))
- zipped_2 = list(list(zip(wire_coordinates_2, steps_2)))
- intersections = set(wire_coordinates_1) & set(wire_coordinates_2)
- print('Part 1: ' + str(min(map(manhattan_distance, intersections))))
- print('Part 2: ' + str(min(map(combined_steps, intersections))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement