-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol7.py
95 lines (62 loc) · 2.13 KB
/
sol7.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
import utils
import re
def initialise():
global luggageRules, totalBags
luggageRules = utils.loadInputFile("input_7.txt")
totalBags = []
def createLookupTable():
global luggageRules, bagLookup
exp = re.compile("^(.*)\\s{1}bags contain\\s{1}(.*)\\.$")
bagLookup = {}
for luggageRule in luggageRules:
m = re.match(exp, luggageRule)
if m:
thisbagLookup = {}
if m.group(2) == "no other bags":
bagLookup[m.group(1)] = thisbagLookup
continue
for xyz in m.group(2).split(", "):
# print(xyz)
res = re.match("^([0-9]+)\\s(.*)\\s(bag|bags)$", xyz)
bagCount = res.group(1)
bagType = res.group(2)
thisbagLookup[bagType] = int(bagCount)
bagLookup[m.group(1)] = thisbagLookup
def findBagsContaining(bags):
global luggageRules, matchedBags
exp = re.compile("^(.*)\\s{1}bags contain.*("+bags+").*$")
parentBags = []
for luggageRule in luggageRules:
m = re.match(exp, luggageRule)
if m:
parentBags.append(m.group(1))
matchedBags.add(m.group(1))
if parentBags: # not empty:
findBagsContaining('|'.join(parentBags))
return parentBags
def findBagsWithin(bag, thisBagCount):
global bagLookup
if len(bagLookup[bag]) == 0:
return thisBagCount
else:
totalBags = 0
for thisBag, countOfBags in bagLookup[bag].items():
totalBags += findBagsWithin(thisBag, countOfBags)
totalBags *= thisBagCount
totalBags += thisBagCount
return totalBags
def part1():
global matchedBags
matchedBags = set()
bags = findBagsContaining("shiny gold")
return len(matchedBags)
def part2():
global bagLookup
totalBags = 0
for thisBag, countOfBags in bagLookup["shiny gold"].items():
totalBags += findBagsWithin(thisBag, countOfBags)
return totalBags
initialise()
createLookupTable()
print("Bags that can hold a shiny gold bag:", part1()) # 197
print("Questions everyone answered yes:", part2()) # 85324