Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import inputParser
- formula = { 'R' : lambda x_y_length: (x_y_length[0] + x_y_length[2], x_y_length[1]),
- 'L' : lambda x_y_length: (x_y_length[0] - x_y_length[2], x_y_length[1]),
- 'U' : lambda x_y_length: (x_y_length[0], x_y_length[1] + x_y_length[2]),
- 'D' : lambda x_y_length: (x_y_length[0], x_y_length[1] - x_y_length[2])}
- def isVertical(vector):
- return vector[0][0] == vector[1][0]
- def isHorizontal(vector):
- return vector[0][1] == vector[1][1]
- def withinRange(value, rangeValues):
- maxValue = max(list(rangeValues))
- minValue = min(list(rangeValues))
- return value > minValue and value < maxValue
- def findIntersectionPoints(vector1, vector2):
- interSectionPoints = None
- if isVertical(vector1) and isHorizontal(vector2) and withinRange(vector1[0][0], (vector2[0][0], vector2[1][0])) and withinRange(vector2[0][1], (vector1[0][1], vector1[1][1])): interSectionPoints = (vector1[0][0], vector2[0][1])
- if isVertical(vector2) and isHorizontal(vector1) and withinRange(vector2[0][0], (vector1[0][0], vector1[1][0])) and withinRange(vector1[0][1], (vector2[0][1], vector2[1][1])): interSectionPoints = (vector2[0][0], vector1[0][1])
- return interSectionPoints
- if __name__ == "__main__":
- wires = map(lambda wire: map(lambda instruction: ((None, None), instruction), wire), inputParser.getListOfPathsSeparatedAsTuple("Day03"))
- wires[0].insert(0, ((0, 0), ('R', 0)))
- wires[1].insert(0, ((0, 0), ('R', 0)))
- for wire in wires:
- for i in range(len(wire) - 1):
- wire[i + 1] = (formula[wire[i + 1][1][0]]((wire[i][0][0], wire[i][0][1], wire[i + 1][1][1])), wire[i + 1][1])
- vectorsInLines = map(lambda wire: [(wire[i][0], wire[i + 1][0]) for i in range(len(wire) - 1)], wires)
- allIntersectionPoints = map(lambda vectorInFirstWire: map(lambda vectorInSecondWire: findIntersectionPoints(vectorInFirstWire, vectorInSecondWire), vectorsInLines[1]), vectorsInLines[0])
- flattenedAndFilteredList = filter(lambda intersectionPoint: not intersectionPoint is None and not intersectionPoint == (0, 0), [intersectionPoint for signleVectorIntersectionList in allIntersectionPoints for intersectionPoint in signleVectorIntersectionList])
- distanceToIntersectionPoint = map(lambda point: abs(point[0]) + abs(point[1]), flattenedAndFilteredList)
- print(min(distanceToIntersectionPoint))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement