private static int CountContainedBags(int count, BagSpec bag)
 {
     foreach (var b in bag.PossibleContents)
     {
         count += CountContainedBags(1, b) * bag.Quantities[b];
     }
     return(count);
 }
        private static void AddPossibleContents(BagSpec bagSpec, string contents)
        {
            if (contents == "no other bags")
            {
                return;
            }

            var items = contents.Split(",").Select(s => s.Trim());

            foreach (var item in items)
            {
                var groups   = ContentsPattern.Match(item).Groups;
                var quantity = Convert.ToInt32(groups[1].ToString());
                var colour   = groups[2].ToString();

                var spec = GetOrCreateBagSpec(colour);
                bagSpec.AddPossibleContents(quantity, spec);
            }
        }
        private static BagSpec GetOrCreateBagSpec(string colour)
        {
            BagSpec spec;
            var     candidates = BagSpecs.Where(b => b.Colour == colour);

            if (candidates.Any())
            {
                spec = candidates.First();
            }
            else
            {
                spec = new BagSpec
                {
                    Colour = colour
                };
                BagSpecs.Add(spec);
            }

            return(spec);
        }
 public void AddPossibleContents(int quantity, BagSpec spec)
 {
     PossibleContents.Add(spec);
     Quantities[spec] = quantity;
 }