Day 21: Allergen Assessment

Classes:

Identifier(value)

Represent an ingredient or allergen identifier.

Allergen(value)

Represent an identifier of an allergen.

Ingredient(value)

Represent an identifier of an ingredient.

IngredientLine(line)

Specify a well-formed line representing an ingredient list with the allergen.

Entry(ingredients, allergens)

Represent an entry in the list of foods.

Data:

INGREDIENT_LINE_RE

Express a line specifying an ingredient list along with the allergens.

Functions:

parse_ingredient_line(line)

Encapsulate the parsing of line into entries.

serialize_entry(entry)

Serialize the entry back into the string.

find_non_allergenic_ingredients(entries)

Find the ingredients without allergens.

solve(lines)

Parse the input and return the set of ingredients without allergens.

class Identifier(value: str)[source]

Represent an ingredient or allergen identifier.

Methods:

__new__(cls, value)

Enforce the properties on the identifier.

static __new__(cls, value: str) Identifier[source]

Enforce the properties on the identifier.

Requires
  • re.fullmatch(r"^[a-zA-Z]+Z", value)

class Allergen(value: str)[source]

Represent an identifier of an allergen.

class Ingredient(value: str)[source]

Represent an identifier of an ingredient.

INGREDIENT_LINE_RE = re.compile('\\s*(?P<ingredients>[a-zA-Z]+(\\s+[a-zA-Z]+)*)\\s+\\(contains\\s+(?P<allergens>[a-zA-Z]+(\\s*,\\s*[a-zA-Z]+)*)\\)')

Express a line specifying an ingredient list along with the allergens.

class IngredientLine(line: str)[source]

Specify a well-formed line representing an ingredient list with the allergen.

Methods:

__new__(cls, line)

requires

static __new__(cls, line: str) IngredientLine[source]
Requires
  • INGREDIENT_LINE_RE.fullmatch(line)

class Entry(ingredients: List[Ingredient], allergens: List[Allergen])[source]

Represent an entry in the list of foods.

Methods:

__init__(ingredients, allergens)

Initialize with the given values.

Attributes:

ingredients

Ingredients of the entry

allergens

Allergens of the entry

__init__(ingredients: List[Ingredient], allergens: List[Allergen]) None[source]

Initialize with the given values.

Requires
  • len(ingredients) > 0

  • len(allergens) > 0

ingredients: List[Ingredient]

Ingredients of the entry

allergens: List[Allergen]

Allergens of the entry

parse_ingredient_line(line: IngredientLine) Entry[source]

Encapsulate the parsing of line into entries.

serialize_entry(entry: Entry) IngredientLine[source]

Serialize the entry back into the string.

Ensures
  • result == serialize_entry(parse_ingredient_line(result))

find_non_allergenic_ingredients(entries: List[Entry]) Set[Ingredient][source]

Find the ingredients without allergens.

solve(lines: List[IngredientLine]) Set[Ingredient][source]

Parse the input and return the set of ingredients without allergens.