From 4dec21f362c03136e9811a4f4c162fcd8c50544e Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Thu, 2 Dec 2021 17:39:03 +0100 Subject: Added day 10 --- day7/__init__.py | 58 -------------------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 day7/__init__.py (limited to 'day7/__init__.py') diff --git a/day7/__init__.py b/day7/__init__.py deleted file mode 100644 index 375c67a..0000000 --- a/day7/__init__.py +++ /dev/null @@ -1,58 +0,0 @@ -import re -from functools import lru_cache -from typing import Iterator, Any, Generator, Dict - -from aoc import BaseAssignment - -rule_matcher = re.compile( - r'(?P\w+\ \w+) bags contain (?Pno other bags|(((, )?\d \w+\ \w+ bags?)+))\.') -contains_matcher = re.compile(r'(?P\d+) (?P\w+\ \w+) bags?') - - -class Assignment(BaseAssignment): - def get_bag_contents(self, contents: str) -> dict: - content_dict = {} - if contents != 'no other bags': - for content in contents.split(', '): - count = contains_matcher.match(content).groupdict() - content_dict[count['bag']] = int(count['count']) - - return content_dict - - def read_input(self, example=False) -> Dict: - self.mapping = {} - - for line in super().read_input(example): - match = rule_matcher.match(line) - rule = match.groupdict() - self.mapping[rule['bag']] = self.get_bag_contents(rule['contains']) - - -class AssignmentOne(Assignment): - @lru_cache - def contains_shiny_gold_bag(self, bag): - if 'shiny gold' in self.mapping[bag]: - return True - - return any([ - self.contains_shiny_gold_bag(containing_bag) - for containing_bag in self.mapping[bag].keys() - ]) - - def run(self, input) -> Any: - nr_of_possibilities = 0 - for bag, contains in self.mapping.items(): - if self.contains_shiny_gold_bag(bag): - nr_of_possibilities += 1 - return nr_of_possibilities - - -class AssignmentTwo(Assignment): - def nr_of_bags_inside(self, bag): - return sum([ - count + (count * self.nr_of_bags_inside(containing_bag)) - for containing_bag, count in self.mapping[bag].items() - ]) - - def run(self, input) -> Any: - return self.nr_of_bags_inside('shiny gold') -- cgit v1.2.3