-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_07a.py
46 lines (35 loc) · 1.36 KB
/
day_07a.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
#!/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_outer_bag(is_contained_by, colour, outer_bag_set):
if colour in is_contained_by.keys():
for bag in is_contained_by[colour]:
if bag not in outer_bag_set:
outer_bag_set.add(bag)
n_outer_bag(is_contained_by, bag, outer_bag_set)
def main():
f = open("../input/day_07_input")
is_contained_by = 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
for bag in bags_contained.split(", "):
bag_nc = getNumberAndColour(bag)
if bag_nc[1] not in is_contained_by.keys():
is_contained_by[bag_nc[1]] = list()
is_contained_by[bag_nc[1]].append(outer_bag)
outer_bag_set = set()
n_outer_bag(is_contained_by, "shiny gold", outer_bag_set)
print(len(outer_bag_set))
if __name__ == "__main__":
main()