Advertisement
Guest User

day3

a guest
Dec 8th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import inputParser
  2.  
  3. formula = { 'R' : lambda x_y_length: (x_y_length[0] + x_y_length[2], x_y_length[1]),
  4.             'L' : lambda x_y_length: (x_y_length[0] - x_y_length[2], x_y_length[1]),
  5.             'U' : lambda x_y_length: (x_y_length[0], x_y_length[1] + x_y_length[2]),
  6.             'D' : lambda x_y_length: (x_y_length[0], x_y_length[1] - x_y_length[2])}
  7.  
  8. def isVertical(vector):
  9.     return vector[0][0] == vector[1][0]
  10.  
  11. def isHorizontal(vector):
  12.     return vector[0][1] == vector[1][1]
  13.  
  14. def withinRange(value, rangeValues):
  15.     maxValue = max(list(rangeValues))
  16.     minValue = min(list(rangeValues))
  17.     return value > minValue and value < maxValue
  18.  
  19. def findIntersectionPoints(vector1, vector2):
  20.     interSectionPoints = None
  21.     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])
  22.     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])
  23.     return interSectionPoints
  24.  
  25. if __name__ == "__main__":
  26.     wires = map(lambda wire: map(lambda instruction: ((None, None), instruction), wire), inputParser.getListOfPathsSeparatedAsTuple("Day03"))
  27.     wires[0].insert(0, ((0, 0), ('R', 0)))
  28.     wires[1].insert(0, ((0, 0), ('R', 0)))
  29.     for wire in wires:
  30.         for i in range(len(wire) - 1):
  31.             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])
  32.     vectorsInLines = map(lambda wire: [(wire[i][0], wire[i + 1][0]) for i in range(len(wire) - 1)], wires)
  33.     allIntersectionPoints = map(lambda vectorInFirstWire: map(lambda vectorInSecondWire: findIntersectionPoints(vectorInFirstWire, vectorInSecondWire), vectorsInLines[1]), vectorsInLines[0])
  34.     flattenedAndFilteredList = filter(lambda intersectionPoint: not intersectionPoint is None and not intersectionPoint == (0, 0), [intersectionPoint for signleVectorIntersectionList in allIntersectionPoints for intersectionPoint in signleVectorIntersectionList])
  35.     distanceToIntersectionPoint = map(lambda point: abs(point[0]) + abs(point[1]), flattenedAndFilteredList)
  36.     print(min(distanceToIntersectionPoint))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement