Пример #1
0
        public void SolveTest()
        {
            string             test     = "Test";
            List <LuggageRule> LugRules = GetLuggageRules(part: test);
            Bag        myBag            = new Bag("shiny", "gold");
            List <Bag> bags             = CountContainedBags(myBag, LugRules);

            Console.WriteLine($"Count of bags: {bags.Count}");
        }
Пример #2
0
 private bool BagInChildTypes(Bag bag, LuggageRule rule)
 {
     foreach (Bag btype in rule.childTypes)
     {
         if (btype == bag)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #3
0
        private List <Bag> CountContainedBags(Bag bag, List <LuggageRule> rules)
        {
            List <Bag> bags = new List <Bag>();

            LuggageRule rule = GetRule(bag, rules);

            if (rule.childTypes.Count > 0)
            {
                foreach ((Bag bg, int ct) in rule.children)
                {
                    for (int i = 0; i < ct; i++)
                    {
                        bags.Add(bg);
                        bags.AddRange(CountContainedBags(bg, rules));
                    }
                }
            }

            return(bags);
        }
Пример #4
0
        public void SolvePart1()
        {
            string             part1      = "1";
            List <LuggageRule> LugRules   = GetLuggageRules(part: part1);
            HashSet <Bag>      Containers = new HashSet <Bag>();
            Bag myBag = new Bag("shiny", "gold");

            List <Bag> bagsToCheck = new List <Bag>()
            {
                myBag
            };

            do
            {
                bagsToCheck = CheckBags(bagsToCheck, ref Containers, LugRules);
            } while (bagsToCheck.Count > 0);

            HashSet <string> colors = Containers.Select(b => b.Name).ToHashSet();

            Console.WriteLine($"Number of possible container colors: {colors.Count}");
        }
Пример #5
0
 private LuggageRule GetRule(Bag bag, List <LuggageRule> LugRules)
 {
     return(LugRules.Where(r => r.parent == bag).First());
 }