-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_a.py
executable file
·116 lines (100 loc) · 3.87 KB
/
part_a.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
import re
from collections import namedtuple
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
2423
"""
recipe_set = RecipeSet.from_recipes_text(_input)
return recipe_set.get_non_allergenic_ingredient_occurrence_count()
class RecipeSet:
@classmethod
def from_recipes_text(cls, recipes_text):
"""
>>> RecipeSet.from_recipes_text(
... "mxmxvkd kfcds sqjhc nhms (contains dairy, fish)\\n"
... "trh fvjkl sbzzf mxmxvkd (contains dairy)\\n"
... "sqjhc fvjkl (contains soy)\\n"
... "sqjhc mxmxvkd sbzzf (contains fish)\\n"
... ).recipes
[Recipe(ingredients=('mxmxvkd', 'kfcds', 'sqjhc', 'nhms'),
allergens=('dairy', 'fish')),
Recipe(ingredients=('trh', 'fvjkl', 'sbzzf', 'mxmxvkd'),
allergens=('dairy',)),
Recipe(ingredients=('sqjhc', 'fvjkl'),
allergens=('soy',)),
Recipe(ingredients=('sqjhc', 'mxmxvkd', 'sbzzf'),
allergens=('fish',))]
"""
return cls(list(map(
Recipe.from_recipe_text, recipes_text.splitlines())))
def __init__(self, recipes):
self.recipes = recipes
def get_non_allergenic_ingredient_occurrence_count(self):
"""
>>> RecipeSet.from_recipes_text(
... "mxmxvkd kfcds sqjhc nhms (contains dairy, fish)\\n"
... "trh fvjkl sbzzf mxmxvkd (contains dairy)\\n"
... "sqjhc fvjkl (contains soy)\\n"
... "sqjhc mxmxvkd sbzzf (contains fish)\\n"
... ).get_non_allergenic_ingredient_occurrence_count()
5
"""
ingredients = self.get_all_ingredients()
allergens_by_ingredient = self.get_allergens_by_ingredient(ingredients)
non_allergenic_ingredients = \
self.get_non_allergenic_ingredients(allergens_by_ingredient)
return sum(
len(non_allergenic_ingredients & set(recipe.ingredients))
for recipe in self.recipes
)
def get_non_allergenic_ingredients(self, allergens_by_ingredient):
return {
ingredient
for ingredient, allergens in allergens_by_ingredient.items()
if not allergens
}
def get_allergens_by_ingredient(self, ingredients):
return {
ingredient: ({
allergen
for recipe in self.recipes
if ingredient in recipe.ingredients
for allergen in recipe.allergens
} - {
allergen
for recipe in self.recipes
if ingredient not in recipe.ingredients
for allergen in recipe.allergens
})
for ingredient in ingredients
}
def get_all_ingredients(self):
return {
ingredient
for recipe in self.recipes
for ingredient in recipe.ingredients
}
class Recipe(namedtuple("Recipe", ("ingredients", "allergens"))):
re_recipe = re.compile(r"^(\w+(?: \w+)*) \(contains (\w+(?:, \w+)*)\)$")
@classmethod
def from_recipe_text(cls, recipe_text):
"""
>>> Recipe.from_recipe_text(
... "mxmxvkd kfcds sqjhc nhms (contains dairy, fish)")
Recipe(ingredients=('mxmxvkd', 'kfcds', 'sqjhc', 'nhms'),
allergens=('dairy', 'fish'))
>>> Recipe.from_recipe_text(
... "sqjhc (contains soy)")
Recipe(ingredients=('sqjhc',), allergens=('soy',))
"""
ingredients_text, allergens_text = \
cls.re_recipe.match(recipe_text).groups()
ingredients = tuple(ingredients_text.split(' '))
allergens = tuple(allergens_text.split(', '))
return cls(ingredients, allergens)
Challenge.main()
challenge = Challenge()