diff options
author | Cody Hiar <cody@hiar.ca> | 2021-02-19 10:01:16 -0700 |
---|---|---|
committer | Cody Hiar <cody@hiar.ca> | 2021-02-19 10:01:16 -0700 |
commit | 9acbf2789a41115a7eb10825693416d13b864ba7 (patch) | |
tree | 363e75a90b01d168e2230e92ecbef174591c05d0 /day7/day7.py | |
parent | 3bb23093ae15aaa2b11c3b3d771d6a9e42b73a0c (diff) |
day 7
Diffstat (limited to 'day7/day7.py')
-rw-r--r-- | day7/day7.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/day7/day7.py b/day7/day7.py index 1f26537..1eb6c57 100644 --- a/day7/day7.py +++ b/day7/day7.py @@ -33,3 +33,36 @@ while True: break else: bags = new_bags + + +mapping = dict() + +# Put bags into dict structure +for section in sections: + parent, child_str = section.split(" bags contain ") + children = [] + if "no other bags" not in child_str: + for child in child_str.replace(".", "").split(", "): + count, adjective, color, _ = prog.match(child).groups() + for x in range(int(count)): + children.append(f"{adjective} {color}") + if parent not in mapping: + mapping[parent] = [] + for child in children: + mapping[parent].append(child) + +bags = list(mapping['shiny gold']) +count = 0 +new_bags = [] +while True: + new_bags = [] + for x in bags: + count += 1 + if mapping[x] == []: + continue + else: + new_bags.extend(mapping[x]) + bags = new_bags + if new_bags == []: + print(count) + break |