Created
December 7, 2017 17:00
-
-
Save 4Kaylum/bfbe0cdef3173467bb5c4342a3a9fb2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node(object): | |
def __init__(self, program): | |
self.raw = program | |
x = program.split(' ') | |
# name, weight (->, program, program..) | |
self.parent = None | |
self.name = x[0] | |
self.weight = int(x[1][1:-1]) | |
self.children = ' '.join(x[3:]).split(', ') | |
self.children_objs = [] | |
self._get_weight_called = False | |
def get_branch_weight(self): | |
return sum(self.get_children_weights() + [self.weight]) | |
def get_children_weights(self): | |
# This is for determining whether to append itself on return | |
self._get_weight_called = True | |
# If it has no children, the branch weight is just itself | |
if self.children == ['']: | |
return [self.weight] | |
# Get the weight of all of its child branches | |
x = [] | |
for i in self.children_objs: | |
x.append(sum(i.get_children_weights())) | |
# If its parent is calling the child weight method, append itself before it returns | |
if not self.parent: | |
self._get_weight_called = False | |
return x | |
if self.parent._get_weight_called: | |
self._get_weight_called = False | |
return x + [self.weight] | |
self._get_weight_called = False | |
return x | |
def get_bad_node(self, goal=None): | |
# Check if it has no children | |
if not self.children_objs: | |
return goal | |
# Get the weight of the child nodes | |
child_weight = self.get_children_weights() | |
# Get the index of the bad weight | |
index = None | |
goal_for_child = None | |
for i in child_weight: | |
if child_weight.count(i) == 1: | |
index = i | |
else: | |
goal_for_child = i | |
print('Index', index) | |
print('Goal', goal) | |
print('Goal for child', goal_for_child) | |
# Get the bad child | |
if index != None: | |
print('There is bad child\n') | |
bad_child = [i for i in self.children_objs if i.get_branch_weight() == index][0] | |
return bad_child.get_bad_node(goal_for_child) | |
else: | |
# The bad node is this one | |
# Goal is what the total needs to be | |
print('This is bad node\n') | |
return self.weight - (self.weight - (goal - sum(self.get_children_weights()))) | |
class Tower(object): | |
def __init__(self): | |
self.nodes = [] | |
self._bottom = None | |
def use_test_data(self): | |
for i in '''pbga (66) | |
xhth (57) | |
ebii (61) | |
havc (66) | |
ktlj (57) | |
fwft (72) -> ktlj, cntj, xhth | |
qoyq (66) | |
padx (45) -> pbga, havc, qoyq | |
tknk (41) -> ugml, padx, fwft | |
jptl (61) | |
ugml (68) -> gyxo, ebii, jptl | |
gyxo (61) | |
cntj (57)'''.split('\n'): | |
self.add(i.strip()) | |
def add(self, program): | |
x = Node(program) | |
self.nodes.append(x) | |
def __getitem__(self, a): | |
for i in self.nodes: | |
if i.name == a: | |
return i | |
raise IndexError | |
def get_bottom(self): | |
for node in self.nodes: | |
for child in self.nodes: | |
if child.name in node.children: | |
child.parent = node | |
node.children_objs.append(child) | |
for i in self.nodes: | |
if i.parent == None: | |
return i | |
@property | |
def bottom(self): | |
if not self._bottom: | |
self._bottom = self.get_bottom() | |
return self._bottom | |
t = Tower() | |
t.use_test_data() | |
t.bottom.name | |
t.bottom.get_bad_node() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment