-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_07b.py
44 lines (34 loc) · 1.2 KB
/
day_07b.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
#!/usr/bin/python3
def normalizeData(line):
return line.replace("bags", "").replace("bag", "").replace(".", "").strip()
def getNumberAndColour(line):
data = line.split(" ")
number = int(data[0])
colour = (" ".join(data[1:])).strip()
return number, colour
def n_inner_bags(contains, colour):
if colour not in contains.keys():
return 1
else:
count = 0
for bag in contains[colour]:
count += bag[0] * n_inner_bags(contains, bag[1])
return count + 1
def main():
f = open("../input/day_07_input")
contains = dict()
for line in f:
line = line.strip().replace("\n", "").replace("\r", "")
line = normalizeData(line)
outer_bag, bags_contained = line.split("contain")
bags_contained = bags_contained.strip()
outer_bag = normalizeData(outer_bag)
if "no other" in bags_contained:
continue
inner_bags = [getNumberAndColour(bag) for bag in bags_contained.split(", ")]
contains.update({outer_bag: inner_bags})
count = n_inner_bags(contains, "shiny gold") - 1 # -1 to remove the shiny gold bag
print(count)
return count
if __name__ == "__main__":
main()