Exemplo n.º 1
0
        private static void Part2()
        {
            // Count the number of bags inside our bag rule
            BagRule ourBagRule      = allBagRules.Find(r => r.Name == ourBagColour);
            long    totalBagsInRule = BagsWithinRule(ourBagRule, 1);

            Console.WriteLine($"\nPart 2: {totalBagsInRule}");
        }
Exemplo n.º 2
0
        private static long BagsWithinRule(BagRule rule, int quantity)
        {
            long result = rule.Quantity;

            foreach (var bag in rule.BagContents)
            {
                result += quantity * bag.Quantity;
                BagRule thisRule = allBagRules.Find(r => r.Name == bag.Name);
                result += quantity * BagsWithinRule(thisRule, bag.Quantity);
            }
            return(result);
        }
Exemplo n.º 3
0
        public static int CanHoldBagColor(BagRule[] allRules, BagRule currentRule, Color colorToCheck)
        {
            if (currentRule.AllowedChildBags.Any(b => b.BagColor == colorToCheck))
            {
                return(1);
            }

            return(currentRule
                   .AllowedChildBags
                   .Aggregate(
                       0,
                       (acc, cr) => acc + CanHoldBagColor(allRules, allRules.Single(r => r.BagColor == cr.BagColor), colorToCheck)
                       ));
        }
Exemplo n.º 4
0
        public static int CountContainedBags
        (
            IReadOnlyDictionary <Color, BagRule> allRules,
            BagRule currentRule
        )
        {
            int childSum =
                currentRule
                .AllowedChildBags
                .SelectMany(cr => Enumerable.Repeat(allRules[cr.BagColor], cr.Amount))
                .Select(r => CountContainedBags(allRules, r) + 1)
                .Sum();

            return(childSum);
        }
Exemplo n.º 5
0
        private static bool RuleContainsBag(BagRule rule, string bagName)
        {
            bool result = false;

            if (rule.Name == bagName)
            {
                return(true);
            }

            foreach (var bag in rule.BagContents)
            {
                BagRule thisRule = allBagRules.Find(r => r.Name == bag.Name);
                result = RuleContainsBag(thisRule, bagName);
                if (result)
                {
                    return(true);
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        private static void CreateBagRules()
        {
            allBagRules = new List <BagRule>();

            foreach (var line in input)
            {
                // Remove words bag and bags and Replace 'contains' with ":"
                string newLine = line.Replace(".", "").Replace("bags", "bag").Replace("bag", "").Replace("contain", ":");

                //Console.WriteLine($"\n{newLine}");

                // Split on ':'
                string[] thisLine   = newLine.Split(':');
                string   thisColour = thisLine[0].Trim();

                // Split on ','
                string[] contains = thisLine[1].Split(',');

                BagRule newBag = new BagRule();
                newBag.Name = thisColour;
                //Console.WriteLine($"{newBag.Name}");

                foreach (var bag in contains)
                {
                    string thisContents = bag.Trim();

                    if (thisContents != "no other")
                    {
                        string quantity = thisContents.Substring(0, thisContents.IndexOf(' ')).Trim();
                        string name     = thisContents.Substring(thisContents.IndexOf(' ')).Trim();
                        newBag.BagContents.Add(new BagRule(name, quantity));
                        //Console.WriteLine($"{quantity} - {name}");
                    }
                }

                allBagRules.Add(newBag);
            }
        }