-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.py
136 lines (103 loc) · 3.66 KB
/
day6.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import argparse
parser = argparse.ArgumentParser(description="AoC day 6")
parser.add_argument("file", help="The file that should be sourced")
parser.add_argument("-p", "--phase", help="The part of the exercise that we are at", type=int, default=1)
parser.add_argument("-t", "--target", help="A target value being aimed for", type=int, default=0)
def main(argv):
print(f"test, {argv}")
infile = open(argv.file, "r")
allItems = []
for line in infile:
# do a line operation
if line:
allItems.append(line)
infile.close()
# commands require a little more processing
betterCommands = processCommands(allItems)
tree = buildTree(betterCommands)
# for name in tree.keys():
# print(f"{tree[name]}")
if argv.phase == 1:
sol = solutionPt1(tree)
print(f"The total number of orbits is {sol}")
elif argv.phase == 2:
sol = solutionPt2(tree)
print(f"The min number of orbital transfers is: {len(sol) - 3}")
class Planet:
def __init__(self, name):
self.name = name
self.children = []
self.searched = False
self.parent = None
def addChild(self, child):
self.children.append(child)
if child.parent is not None:
print(f"something is wrong with parent {self.name} and child {child.name}")
else:
child.parent = self
def __str__(self):
str = f"+ {self.name} ++++++++++++++++++"
if self.parent is not None:
str += f"\nParent: {self.parent.name}"
if len(self.children) == 0:
str += "\n=== LEAF ==="
elif len(self.children) > 0:
for child in self.children:
str += f"\n--- {child.name}"
elif self.parent is None:
str += "\n========== ROOT ============="
return str
def processCommands(commands):
splitCommands = []
for command in commands:
commandSet = str.split(command, ')')
commandSet[1] = commandSet[1].strip('\n')
splitCommands.append(commandSet)
return splitCommands
def buildTree(commands):
allPlanets = {}
# root ) child
for command in commands:
if not command[0] in allPlanets.keys():
allPlanets[command[0]] = Planet(command[0])
if not command[1] in allPlanets.keys():
allPlanets[command[1]] = Planet(command[1])
allPlanets[command[0]].addChild(allPlanets[command[1]])
return allPlanets
def solutionPt1(planetTree):
jumps = 0
for planet in planetTree.keys():
#count jumps for each planet
cur_planet = planetTree[planet]
while cur_planet.parent is not None:
jumps += 1
cur_planet = cur_planet.parent
return jumps
def solutionPt2(planetTree):
stack = [planetTree["YOU"]]
temp = planetTree["YOU"]
while stack[-1] != planetTree["SAN"]:
cur = stack[-1]
# all = []
# for i in stack:
# all.append(i.name)
# print(all)
#done with children, go up tree
nextUnsearched = None
if len(cur.children) > 0:
nextUnsearched = next((i for i in cur.children if not i.searched), None)
if nextUnsearched is None:
# we started low and are still going up
if len(stack) == 1 or cur.parent != stack[-2]:
cur.searched = True
stack.append(cur.parent)
continue
#backtracking
elif cur.parent == stack[-2]:
cur.searched = True
stack.pop()
continue
# dive!
stack.append(nextUnsearched)
return stack
main(parser.parse_args())