Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Day 21 - Allergen Assessment");
            Console.WriteLine("Star 1");
            Console.WriteLine();

            List <Food> foods = File.ReadAllLines(inputFile)
                                .Select(x => new Food(x))
                                .ToList();

            List <string> allergenTexts = foods
                                          .SelectMany(x => x.allergens)
                                          .Distinct()
                                          .ToList();

            HashSet <string> uniqueIngredients = new HashSet <string>(foods.SelectMany(x => x.ingredients));

            List <Allergen> allergens = new List <Allergen>();

            foreach (string allergenText in allergenTexts)
            {
                Allergen allergen = new Allergen(allergenText);

                foreach (Food food in foods.Where(x => x.allergens.Contains(allergenText)))
                {
                    allergen.AddIngredientCollection(food.ingredients);
                }

                allergens.Add(allergen);
            }


            bool finished = false;
            HashSet <Allergen> figuredOutAllergens = new HashSet <Allergen>();

            while (!finished)
            {
                finished = true;

                foreach (Allergen allergen in allergens)
                {
                    if (allergen.IsFiguredOut &&
                        !figuredOutAllergens.Contains(allergen))
                    {
                        foreach (Allergen removeTarget in allergens.Where(x => x != allergen))
                        {
                            removeTarget.RemoveIngredient(allergen.MyIngredient);
                        }

                        figuredOutAllergens.Add(allergen);

                        finished = false;
                    }
                }
            }

            HashSet <string> nonAllergenIngredients =
                new HashSet <string>(uniqueIngredients);

            foreach (Allergen allergen in allergens)
            {
                nonAllergenIngredients.ExceptWith(allergen.ingredientCollection);
            }

            int output1 = foods
                          .SelectMany(x => x.ingredients)
                          .Where(x => nonAllergenIngredients.Contains(x))
                          .Count();

            Console.WriteLine($"The answer is: {output1}");

            Console.WriteLine();
            Console.WriteLine("Star 2");
            Console.WriteLine();

            Console.WriteLine($"The answer is: {string.Join(',', allergens.OrderBy(x => x.name).Select(x => x.MyIngredient))}");


            Console.WriteLine();
            Console.ReadKey();
        }