示例#1
0
        public static long GetAmountOfBagsRecursively(BagRule parent, List <BagRule> ruleSet)
        {
            long amount = 0;

            foreach (var childBag in parent.ChildrenBags)
            {
                amount += childBag.Amount;

                BagRule childBagRule = ruleSet.Find(s => s.Bag.Equals(childBag));
                amount += childBag.Amount * GetAmountOfBagsRecursively(childBagRule, ruleSet);
            }

            return(amount);
        }
示例#2
0
        public static bool ContainsRecursively(BagRule parent, Bag search, List <BagRule> ruleSet)
        {
            if (parent.Bag.Equals(search))
            {
                return(false);
            }

            if (parent.ChildrenBags.Contains(search))
            {
                return(true);
            }

            foreach (var childBags in parent.ChildrenBags)
            {
                BagRule childBagRule = ruleSet.Find(s => s.Bag.Equals(childBags));
                if (ContainsRecursively(childBagRule, search, ruleSet))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#3
0
        static void Main(string[] args)
        {
            string[] lines = File.ReadAllLines("input.txt");

            List <BagRule> bagMapping = new List <BagRule>();

            foreach (var line in lines)
            {
                var rule = ParseRule(line);
                bagMapping.Add(rule);
                //Console.WriteLine(rule.Bag.Name + ": " + string.Join('/', rule.ChildrenBags.Select(b => $"({b.Amount}x) {b.Name}")));
            }

            Bag shinyGoldBag = new Bag()
            {
                Name = "shiny gold bag"
            };

            BagRule shinyGoldBagRule = bagMapping.Find(s => s.Bag.Equals(shinyGoldBag));

            long shinyBagCanContainAmountOfBags = GetAmountOfBagsRecursively(shinyGoldBagRule, bagMapping);

            Console.WriteLine($"Amount of bags that a shiny gold bag can contain: {shinyBagCanContainAmountOfBags}");
        }