Safe HaskellNone

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

Documentation

type Allergene = String #

type Ingredient = String #

type Foods = Map Allergene [[Ingredient]] #

input :: String -> Foods #

Read the input file and return map of foods and ingredients.

intersect :: Eq a => [a] -> [a] -> [a] #

Intersect two lists. Return common elements.

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.

diffr :: Eq a => [a] -> [a] -> [a] #

Build the right diff between two lists. Return the diff.

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.

part1 :: Foods -> Int #

Solve part1.

ingredientContainsAllergene :: Foods -> [(Ingredient, Allergene)] #

Go through all foods and find the ingredients that contains allergenes.

part2 :: Foods -> String #

Solve part2.