diff options
Diffstat (limited to 'day7/__init__.py')
| -rw-r--r-- | day7/__init__.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/day7/__init__.py b/day7/__init__.py new file mode 100644 index 0000000..375c67a --- /dev/null +++ b/day7/__init__.py | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | import re | ||
| 2 | from functools import lru_cache | ||
| 3 | from typing import Iterator, Any, Generator, Dict | ||
| 4 | |||
| 5 | from aoc import BaseAssignment | ||
| 6 | |||
| 7 | rule_matcher = re.compile( | ||
| 8 | r'(?P<bag>\w+\ \w+) bags contain (?P<contains>no other bags|(((, )?\d \w+\ \w+ bags?)+))\.') | ||
| 9 | contains_matcher = re.compile(r'(?P<count>\d+) (?P<bag>\w+\ \w+) bags?') | ||
| 10 | |||
| 11 | |||
| 12 | class Assignment(BaseAssignment): | ||
| 13 | def get_bag_contents(self, contents: str) -> dict: | ||
| 14 | content_dict = {} | ||
| 15 | if contents != 'no other bags': | ||
| 16 | for content in contents.split(', '): | ||
| 17 | count = contains_matcher.match(content).groupdict() | ||
| 18 | content_dict[count['bag']] = int(count['count']) | ||
| 19 | |||
| 20 | return content_dict | ||
| 21 | |||
| 22 | def read_input(self, example=False) -> Dict: | ||
| 23 | self.mapping = {} | ||
| 24 | |||
| 25 | for line in super().read_input(example): | ||
| 26 | match = rule_matcher.match(line) | ||
| 27 | rule = match.groupdict() | ||
| 28 | self.mapping[rule['bag']] = self.get_bag_contents(rule['contains']) | ||
| 29 | |||
| 30 | |||
| 31 | class AssignmentOne(Assignment): | ||
| 32 | @lru_cache | ||
| 33 | def contains_shiny_gold_bag(self, bag): | ||
| 34 | if 'shiny gold' in self.mapping[bag]: | ||
| 35 | return True | ||
| 36 | |||
| 37 | return any([ | ||
| 38 | self.contains_shiny_gold_bag(containing_bag) | ||
| 39 | for containing_bag in self.mapping[bag].keys() | ||
| 40 | ]) | ||
| 41 | |||
| 42 | def run(self, input) -> Any: | ||
| 43 | nr_of_possibilities = 0 | ||
| 44 | for bag, contains in self.mapping.items(): | ||
| 45 | if self.contains_shiny_gold_bag(bag): | ||
| 46 | nr_of_possibilities += 1 | ||
| 47 | return nr_of_possibilities | ||
| 48 | |||
| 49 | |||
| 50 | class AssignmentTwo(Assignment): | ||
| 51 | def nr_of_bags_inside(self, bag): | ||
| 52 | return sum([ | ||
| 53 | count + (count * self.nr_of_bags_inside(containing_bag)) | ||
| 54 | for containing_bag, count in self.mapping[bag].items() | ||
| 55 | ]) | ||
| 56 | |||
| 57 | def run(self, input) -> Any: | ||
| 58 | return self.nr_of_bags_inside('shiny gold') | ||
