Guest User

Untitled

a guest
Dec 2nd, 2019
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with open('input3') as input:
  2. lines = input.readlines()
  3.  
  4. # Part 1
  5. wires = [[],[]]
  6. minmatch = float('inf')
  7. for i, wire in enumerate(lines):
  8. goes = wire.split(',')
  9. x = 0
  10. y = 0
  11. for gi, go in enumerate(goes):
  12. print(gi)
  13. direction = go[0]
  14. distance = int(go[1:])
  15. if direction == 'R':
  16. for _ in range(distance):
  17. x += 1
  18. wires[i].append((x,y))
  19. if i == 1 and abs(x)+abs(y) < minmatch and (x,y) in wires[0]:
  20. minmatch = abs(x)+abs(y)
  21. elif direction == 'L':
  22. for _ in range(distance):
  23. x -= 1
  24. wires[i].append((x,y))
  25. if i == 1 and abs(x)+abs(y) < minmatch and (x,y) in wires[0]:
  26. minmatch = abs(x)+abs(y)
  27.  
  28. elif direction == 'U':
  29. for _ in range(distance):
  30. y += 1
  31. wires[i].append((x,y))
  32. if i == 1 and abs(x)+abs(y) < minmatch and (x,y) in wires[0]:
  33. minmatch = abs(x)+abs(y)
  34. elif direction == 'D':
  35. for _ in range(distance):
  36. y -= 1
  37. wires[i].append((x,y))
  38. if i == 1 and abs(x)+abs(y) < minmatch and (x,y) in wires[0]:
  39. minmatch = abs(x)+abs(y)
  40. print('p1:', minmatch)
  41.  
  42. # Part 2
  43. wires = [[],[]]
  44. d = {}
  45. minmatch = float('inf')
  46. for i, wire in enumerate(lines):
  47. goes = wire.split(',')
  48. x = 0
  49. y = 0
  50. steps = 0
  51. for gi, go in enumerate(goes):
  52. print(gi)
  53. direction = go[0]
  54. distance = int(go[1:])
  55. if direction == 'R':
  56. for _ in range(distance):
  57. x += 1
  58. steps += 1
  59. if i == 0:
  60. d[str(x) + ';' + str(y)] = steps
  61. wires[i].append((x,y))
  62. try:
  63. if i == 1 and steps + d[str(x) + ';' + str(y)] < minmatch and (x,y) in wires[0]:
  64. minmatch = steps + d[str(x) + ';' + str(y)]
  65. except:
  66. pass
  67. elif direction == 'L':
  68. for _ in range(distance):
  69. x -= 1
  70. steps += 1
  71. if i == 0:
  72. d[str(x) + ';' + str(y)] = steps
  73.  
  74. wires[i].append((x,y))
  75. try:
  76. if i == 1 and steps + d[str(x) + ';' + str(y)] < minmatch and (x,y) in wires[0]:
  77. minmatch = steps + d[str(x) + ';' + str(y)]
  78. except:
  79. pass
  80. elif direction == 'U':
  81. for _ in range(distance):
  82. y += 1
  83. steps += 1
  84. if i == 0:
  85. d[str(x) + ';' + str(y)] = steps
  86. wires[i].append((x,y))
  87. try:
  88. if i == 1 and steps + d[str(x) + ';' + str(y)] < minmatch and (x,y) in wires[0]:
  89. minmatch = steps + d[str(x) + ';' + str(y)]
  90. except:
  91. pass
  92. elif direction == 'D':
  93. for _ in range(distance):
  94. y -= 1
  95. steps += 1
  96. if i == 0:
  97. d[str(x) + ';' + str(y)] = steps
  98. wires[i].append((x,y))
  99. try:
  100. if i == 1 and steps + d[str(x) + ';' + str(y)] < minmatch and (x,y) in wires[0]:
  101. minmatch = steps + d[str(x) + ';' + str(y)]
  102. except:
  103. pass
  104. print('p2:',minmatch)
RAW Paste Data