-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.py
65 lines (54 loc) · 1.86 KB
/
day24.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def readInput(filename):
positions = []
with open(filename, 'r') as file:
for eachLine in file:
splitted = eachLine.strip().split('/')
positions.append([int(splitted[0]), int(splitted[1])])
return positions
def findPaths(contextPath, target, positions):
allPaths = []
extendPath = False
for i in range(len(positions)):
if positions[i][0] == target or positions[i][1] == target:
extendPath = True
newTarget = positions[i][1] if positions[i][0] == target else positions[i][0]
allPaths += findPaths(contextPath+[positions[i]], newTarget, positions[:i]+positions[i+1:])
if not extendPath:
allPaths.append(contextPath)
return allPaths
def allPaths(allPos):
allPaths = []
for i in range(len(allPos)):
if allPos[i][0] == 0:
paths = findPaths([allPos[i]], allPos[i][1], allPos[:i]+allPos[i+1:])
for eachPath in paths:
allPaths.append(eachPath)
return allPaths
def maxPath(paths):
allPathV = []
for eachPath in paths:
pathV = 0
for eachElem in eachPath:
pathV += eachElem[0]
pathV += eachElem[1]
allPathV.append(pathV)
return max(allPathV)
def longestPaths(paths):
longest = 0
longestPaths = []
for eachPath in paths:
if len(eachPath) > longest:
longestPaths = [eachPath]
longest = len(eachPath)
elif len(eachPath) == longest:
longestPaths.append(eachPath)
return longestPaths
#allPos=[[0,2],[2,2],[2,3],[3,4],[3,5],[0,1],[10,1],[9,10]]
#print(maxPath(longestPaths(allPaths(allPos))))
def Solution1():
allPos = readInput('day24Input.txt')
print(maxPath(allPaths(allPos)))
def Solution2():
allPos = readInput('day24Input.txt')
print(maxPath(longestPaths(allPaths(allPos))))
Solution2()