"""Solution for 2020/21""" from functools import reduce foods = [] all_ingredients = set() all_allergenes = set() with open('input', 'r') as f: for line in f.readlines(): [ing, alg] = line.strip(' \n)').split(' (contains ') ing = set(ing.split(' ')) alg = set(alg.split(', ')) all_ingredients = all_ingredients.union(ing) all_allergenes = all_allergenes.union(alg) foods.append((ing, alg)) allergeneMap = {} for ingredients, allergenes in foods: for allergene in allergenes: if allergene not in allergeneMap: allergeneMap[allergene] = ingredients else: allergeneMap[allergene] = allergeneMap[allergene].intersection( ingredients) print(f'{allergeneMap=}') allergenic = reduce(lambda x, y: x.union(y), allergeneMap.values()) unallergenic = all_ingredients.difference(allergenic) print(f'{allergenic=}') print(f'{unallergenic=}') solution = 0 for ingredients, _ in foods: solution += len(ingredients.difference(allergenic)) print( f'{ingredients} - {allergenic} = {len(ingredients.difference(allergenic))}') print(solution)