joe!
This commit is contained in:
19
day7/day7.py
19
day7/day7.py
@@ -16,13 +16,13 @@ def gettree():
|
|||||||
if words[4] == 'no':
|
if words[4] == 'no':
|
||||||
continue
|
continue
|
||||||
if index == 0:
|
if index == 0:
|
||||||
mBag = words[5] + " " + words[6]
|
rule = words[5] + " " + words[6]
|
||||||
mNum = int(words[4])
|
num = int(words[4])
|
||||||
tree[top_bag][mBag] = mNum
|
tree[top_bag][rule] = num
|
||||||
else:
|
else:
|
||||||
mBag = words[2] + " " + words[3]
|
rule = words[2] + " " + words[3]
|
||||||
mNum = int(words[1])
|
num = int(words[1])
|
||||||
tree[top_bag][mBag] = mNum
|
tree[top_bag][rule] = num
|
||||||
|
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
@@ -35,15 +35,14 @@ def countShine(bag, tree):
|
|||||||
sum = 0
|
sum = 0
|
||||||
for child in tree[bag]:
|
for child in tree[bag]:
|
||||||
sum += countShine(child, tree)
|
sum += countShine(child, tree)
|
||||||
|
|
||||||
return sum
|
return sum
|
||||||
|
|
||||||
|
|
||||||
def countBags(bag, tree):
|
def countBags(bag, tree):
|
||||||
item = tree[bag]
|
rule = tree[bag]
|
||||||
sum = 0
|
sum = 0
|
||||||
for child in item:
|
for child in rule:
|
||||||
sum += item[child] + (item[child] * countBags(child, tree))
|
sum += rule[child] + (rule[child] * countBags(child, tree))
|
||||||
|
|
||||||
return sum
|
return sum
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user