public int TotalBags(string bagColor, int depth = 0)
            {
                if (!Rules.ContainsKey(bagColor))
                {
                    return(0);
                }

                BagRule bagRule = Rules[bagColor];

                //if (bagRule.ContainsRules.Count == 0)
                //	return 1;

                int count = 1;

                if (depth == 0)
                {
                    count = 0;
                }

                foreach (Contains b in bagRule.ContainsRules)
                {
                    count += TotalBags(b.Color, depth + 1) * b.Quantity;
                }
                return(count);
            }
            public int ContainsBags(string bagColor, string findBagColor)
            {
                if (!Rules.ContainsKey(bagColor))
                {
                    return(0);
                }

                BagRule bagRule = Rules[bagColor];

                if (bagRule.BagColorCount.ContainsKey(findBagColor))
                {
                    return(bagRule.BagColorCount[findBagColor]);
                }
                else
                {
                    int count = 0;
                    foreach (Contains containedRule in bagRule.ContainsRules)
                    {
                        if (containedRule.Color == findBagColor)
                        {
                            return(containedRule.Quantity);
                        }

                        count += ContainsBags(containedRule.Color, findBagColor);
                    }
                    bagRule.BagColorCount.Add(findBagColor, count);
                    return(count);
                }
            }
 public void ParseRules(List <string> rules)
 {
     foreach (string rule in rules)
     {
         BagRule newRule = new BagRule(rule);
         Rules.Add(newRule.Color, newRule);
     }
 }
Exemple #4
0
        public static int GetBagsInsideBag(List <BagRule> rules, BagRule currentRule)
        {
            var sum = 0;

            for (int ii = 0; ii < currentRule.Contains.Length; ii++)
            {
                var containsBag = currentRule.Contains[ii];
                var countBag    = currentRule.ContainsCount[ii];

                sum += countBag;
                var bagRule = rules.First(rule => rule.BagName == containsBag);
                sum += countBag * GetBagsInsideBag(rules, bagRule);
            }
            return(sum);
        }
Exemple #5
0
        public static void Part1()
        {
            var inputStrings = File.ReadAllLines(inputFilePath);
            //var inputStrings = testInput;

            var rules = new List <BagRule>();

            foreach (var ruleString in inputStrings)
            {
                var bagRule = new BagRule(ruleString);
                Console.WriteLine(bagRule);
                rules.Add(bagRule);
            }

            //Okay, now we've got the rules:
            var goalBag = "shiny gold";

            var goalBags = new List <string>()
            {
                goalBag
            };
            var bagsThatCouldContainGoalBag = new HashSet <string>();

            while (goalBags.Count > 0)
            {
                var currentGoalBag = goalBags[0];
                goalBags.RemoveAt(0);
                var bagsContainingGoalBag = GetBagsContainingGoalBag(rules, currentGoalBag);
                foreach (var bagContainingGoalBag in bagsContainingGoalBag)
                {
                    bagsThatCouldContainGoalBag.Add(bagContainingGoalBag);
                }
                goalBags.AddRange(bagsContainingGoalBag);
            }

            Console.WriteLine("");
            Console.WriteLine($"Could Contain Bags: {string.Join(", ", bagsThatCouldContainGoalBag)}");
            Console.WriteLine($"Count: {bagsThatCouldContainGoalBag.Count()}");
        }
        public static List <BagRule> GetMappedBagRules(List <string> bagRules)
        {
            var result = new List <BagRule>();

            foreach (var bagRule in bagRules)
            {
                var rule = new BagRule();

                var bagRuleMainSplit = Regex.Split(bagRule, "contain");

                rule.Description   = bagRuleMainSplit[0].Trim().Split(" ".ToCharArray())[0] + " " + bagRuleMainSplit[0].Trim().Split(" ".ToCharArray())[1];
                rule.ContainedBags = new Dictionary <string, int>();

                var containedBagsSplit = bagRuleMainSplit[1].Trim().Split(",".ToCharArray());

                foreach (var containedBagSplit in containedBagsSplit)
                {
                    var spaceSplit = containedBagSplit.Trim().Split(" ".ToCharArray());

                    if (spaceSplit[0] == "no")
                    {
                        break;
                    }

                    //var containedBag = new ContainedBag
                    //{
                    //    Quantity = int.Parse(spaceSplit[0]),
                    //    Description = spaceSplit[1] + " " + spaceSplit[2]
                    //};

                    rule.ContainedBags.Add(spaceSplit[1] + " " + spaceSplit[2], int.Parse(spaceSplit[0]));
                }

                result.Add(rule);
            }

            return(result);
        }
Exemple #7
0
        public static void Part2()
        {
            var inputStrings = File.ReadAllLines(inputFilePath);
            //var inputStrings = testInput;
            //var inputStrings = testInput2;

            var rules = new List <BagRule>();

            foreach (var ruleString in inputStrings)
            {
                var bagRule = new BagRule(ruleString);
                Console.WriteLine(bagRule);
                rules.Add(bagRule);
            }

            //Okay, now we've got the rules:
            var currentBagString = "shiny gold";
            var currentRule      = rules.First(rule => rule.BagName == currentBagString);
            var bagCount         = GetBagsInsideBag(rules, currentRule);

            Console.WriteLine("");
            Console.WriteLine($"Final Count: {bagCount}");
        }