Safe Haskell | None |
---|
Day21
Description
General - Analyse food. Food has incredients. A given allergene is in one-of the ingredients.
Part 1 - Build the intersections to find out which incredients do have allergens. Find more by looking for allergens with only one ingredient (search by elimination; sudoku strategy). You need to do theses two steps/phases recursively until no more "singletons" can be found.
Part 2 - This takes some more thinking. First we are removing the incredience that we know are free of allergens (from part1).
Then we need to see that we can remove allergens that only have one ingredient. We do this recursively until no more foods are left over.
Synopsis
- type Allergene = String
- type Ingredient = String
- type Foods = Map Allergene [[Ingredient]]
- input :: String -> Foods
- intersect :: Eq a => [a] -> [a] -> [a]
- intersect' :: Eq a => [[a]] -> [a]
- intersectIngredience :: Foods -> Foods
- diffr :: Eq a => [a] -> [a] -> [a]
- diffMapValues :: Eq v => [v] -> Map k [[v]] -> Map k [[v]]
- isSingleton :: forall a. [a] -> Bool
- isSingleton' :: forall a. [[a]] -> Bool
- removeIngredientsByIntersection :: Foods -> Foods
- removeIngredientsBySingleton :: Foods -> Foods
- foodsFreeOfAllergens :: Foods -> Foods
- allIncredientsFreeOfAllergens :: Foods -> [Ingredient]
- part1 :: Foods -> Int
- ingredientContainsAllergene :: Foods -> [(Ingredient, Allergene)]
- part2 :: Foods -> String
Documentation
type Ingredient = String #
type Foods = Map Allergene [[Ingredient]] #
intersect' :: Eq a => [[a]] -> [a] #
Build the intersection of a list of lists. Return the common elements.
intersectIngredience :: Foods -> Foods #
Intersect the ingredience of all foods.
diffMapValues :: Eq v => [v] -> Map k [[v]] -> Map k [[v]] #
Build a (foods) map and diffr the ingredients.
isSingleton :: forall a. [a] -> Bool #
Return true, if list got only one element.
isSingleton' :: forall a. [[a]] -> Bool #
Return true, if list of lists got only one list and that list got only one element.
removeIngredientsByIntersection :: Foods -> Foods #
Take a map of foods and remove the ingredience that intersect.
removeIngredientsBySingleton :: Foods -> Foods #
Take a map of foods and remove the ingredience that are singletons.
foodsFreeOfAllergens :: Foods -> Foods #
Foods that a free of allergenes.
allIncredientsFreeOfAllergens :: Foods -> [Ingredient] #
All ingredients that are free of allergens.
ingredientContainsAllergene :: Foods -> [(Ingredient, Allergene)] #
Go through all foods and find the ingredients that contains allergenes.