-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
91 lines (83 loc) · 2.75 KB
/
Copy path7.py
File metadata and controls
91 lines (83 loc) · 2.75 KB
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
import os
fileName = "7.txt"
with open(fileName) as file:
rules = {}
total = 0
text = file.readlines()
for rule in text:
total = total + 1
myColor = rule[0:rule.find(" bag")]
i = rule.find("contain") + 7
others = rule[i:-2]
others = others.replace("bags","")
others = others.replace("bag","")
others = others.split(",")
for i in range(len(others)):
others[i] = others[i][3:-1]
rules[myColor] = others
#print(myColor,others)
#print(total, len(rules)) #proves rules are for unique colors
containsGold = {}
containsGold["shiny gold"] = True
#for rule in rules:
# if "shiny gold" in rules[rule]:
# containsGold[rule] = True
size = 0
while size != len(containsGold):
size = len(containsGold)
for foundColor in list(containsGold):
for newColor in rules:
if foundColor in rules[newColor]:
containsGold[newColor] = True
#print(size,"----------------",len(containsGold))
#print(containsGold.keys())
print(len(containsGold) - 1) #remove shiny gold
#part 2
rules = {}
total = 0
def insidePackages(color):
if len(rules[color]) == 0:
return 0
count = 0
for sub in rules[color].keys():
#print ("---",sub,rules[color][sub],insidePackages(sub))
count = count + (rules[color][sub] * (insidePackages(sub) + 1))
#print(color,count)
return count
with open(fileName) as file:
# text = file.readlines()
for rule in text:
total = total + 1
myColor = rule[0:rule.find(" bag")]
i = rule.find("contain") + 7
others = rule[i:-2]
others = others.replace("bags","")
others = others.replace("bag","")
others = others.split(",")
contents = {}
for other in others:
try:
contents[other[3:-1]] = int(other[1])
except:
pass
rules[myColor] = contents
#print(myColor,others)
#print(total, len(rules)) #proves rules are for unique colors
#print("------------")
#print(rules)
#print("------------")
insideGold = {}
for color in rules["shiny gold"]:
insideGold[color] = rules["shiny gold"][color]
#size = 0
#while size != len(insideGold):
# size = len(insideGold)
# for foundColor in list(insideGold):
# for newColor in rules[foundColor].keys():
# insideGold[newColor] = rules[foundColor][newColor]
#print(size,"----------------",len(containsGold))
#print(containsGold.keys())
packages = insidePackages("shiny gold")
#for item in insideGold.keys():
# packages = packages + insideGold[item]
print(packages)