﻿ # Untitled

a guest
Dec 7th, 2019
720
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #Determine current workstation and select cwd accordingly.
2. import os
3. homePCDir = os.path.isfile('C:\\Users\\Jordan\\Google Drive\\AoC\\2019\\Day 3 input.txt')
4. laptopDir = os.path.isfile('C:\\Users\\Jedwa\\Google Drive\\AoC\\2019\\Day 3 input.txt')
5. if homePCDir:
7. elif laptopDir:
9. else:
10.     print("No Input File or Directory")
11.
12. #Function to get instructions as a list
13. def getInst():
14.     with open('Day 3 input.txt') as file:
16.         return inst
17.
18. #Function for creating a coordinate list
19. def appendcoords(x, y, coordinates):
20.     coordinates.append(f'{x}:{y}')
21.     return coordinates
22.
23. #Function for plotting wire path to the coordinate list
24. def wirePath(wireStr):
25.     #Use Regex to split the directions into two sets, one for direction, one for distance
26.     import re
27.     dir = re.findall("[UDLR]", wireStr)
28.     dist = re.findall("[0-9]+", wireStr)
29.
30.     #define starting coordinates and split for loop into different scenarios for x or y changes
31.     x1 = 0
32.     y1 = 0
33.     coordinates = list()
34.     for index, letter in enumerate(dir):
35.         x2 = ""
36.         y2 = ""
37.         if letter == 'R':
38.             x2 = x1 + int(dist[index])
39.             while x1 != x2:
40.                 x1 += 1
41.                 appendcoords(x1, y1, coordinates)
42.         if letter == 'L':
43.             x2 = x1 - int(dist[index])
44.             while x1 != x2:
45.                 x1 -= 1
46.                 appendcoords(x1, y1, coordinates)
47.         if letter == 'U':
48.             y2 = y1 + int(dist[index])
49.             while y1 != y2:
50.                 y1 += 1
51.                 appendcoords(x1, y1, coordinates)
52.         if letter == 'D':
53.             y2 = y1 - int(dist[index])
54.             while y1 != y2:
55.                 y1 -= 1
56.                 appendcoords(x1, y1, coordinates)
57.     return coordinates
58.
59. #Function for finding closest coordinates
60. def manhatDist(funcList):
61.     i = 0
62.     currentNum = False
63.     for x in funcList: #I think there are much better ways to do this but I couldn't figure it out and I don't want to break anything now. particularly I don't know what significance x has at all.
64.         testNum = abs(funcList[i]) + abs(funcList[i+1])
65.         if currentNum == False or testNum < currentNum:
66.             currentNum = testNum
67.         if i + 2 >= len(funcList):
68.             return currentNum
69.         else:
70.             i += 2
71.
72. #function to count steps of a match for two wires
73. def countSteps(matchList, wireList1, wireList2):
74.     #recombine matchList to compare with wire paths; at this point I realize this is obviously not effecient but I've dug a hole that I'll keep digging for now.
75.     combinedMatches = []
76.     i = 0
77.     finalDist = 0
78.     for x in matchList:
79.         combinedMatches.append(f'{matchList[i]}:{matchList[i + 1]}')
80.         if i + 2 >= len(matchList):
81.             break
82.         i += 2
83.     for k in combinedMatches:
84.         for index1, num1 in enumerate(wireList1):
85.             if k == num1:
86.                 dist1 = int(index1 + 1)
87.                 for index2, num2 in enumerate(wireList2):
88.                     if k == num2:
89.                         dist2 = int(index2 + 1)
90.                         currentDist = dist1 + dist2
91.                         if finalDist == 0 or currentDist < finalDist:
92.                             finalDist = currentDist
93.     return finalDist
94.
95. #find matches and return a list of them
96. def findMatch(wirePath1, wirePath2):
97.     matches = set(wirePath1).intersection(set(wirePath2))
98.     matchList = []
99.     while len(matches) > 0:
100.         currentCoord = matches.pop()
101.         tempList = [int(x) for x in currentCoord.split(':')]
102.         matchList.append(tempList)
103.         matchList.append(tempList)
104.     return matchList
105.
106. #Create object for each wire's instructions
107. instList = getInst()
108. firstWire = instList
109. secondWire = instList
110.
111. #lists of coordinates for each wire
112. firstWirePath = wirePath(firstWire)
113. secondWirePath = wirePath(secondWire)
114.
115. #matches
116. matchList = findMatch(firstWirePath, secondWirePath)
117.