Пример #1
0
        public Day07() : base(07, 2020, "")
        {
            foreach (string line in Input.SplitByNewline())
            {
                string[] parts = line.Split("bags contain");
                string[] bags  = parts[1].Replace(".", "").Trim().Split(",");

                OuterBag t = new OuterBag();
                t.color = parts[0].Replace(" bags", "").Trim();
                t.inner = new List <InnerBag>();

                foreach (string inner in bags)
                {
                    if (inner.Trim() == "no other bags")
                    {
                        break;
                    }

                    // First character is a qty, the rest is a bag
                    string[] innerBag = inner.Trim().Split(" ", 2);

                    t.inner.Add(new InnerBag()
                    {
                        qty = Int32.Parse(innerBag[0]), color = innerBag[1].Replace(" bags", "").Replace(" bag", "").Trim()
                    });
                }

                rules.Add(t);
            }
        }
Пример #2
0
        private int bagCount(OuterBag bag)
        {
            // Need to process this rule including ourselve
            int count = 1;

            // How many inner bags?
            foreach (var ib in bag.inner)
            {
                count += bagCountIB(ib);
            }

            return(count);
        }