Beispiel #1
0
        private int Part2()
        {
            BaggageRule shinyGoldRule = ruleMap["shiny gold"];

            int sum = 0;
            Stack <BaggageRule> stack = new Stack <BaggageRule>();

            stack.Push(shinyGoldRule);

            while (stack.Count > 0)
            {
                BaggageRule curRule = stack.Pop();
                sum += curRule.Contents.Values.Sum();

                foreach (BaggageRule key in curRule.Contents.Keys)
                {
                    for (int x = 0; x < curRule.Contents[key]; x++)
                    {
                        stack.Push(key);
                    }
                }
            }

            return(sum);
        }
Beispiel #2
0
        public Day7()
        {
            baggageRules = File.ReadAllLines(@"E:\GitHub\CSharp-Codes\AdventCode\baggages.txt");
            Regex ruleStart    = new Regex("(.*?) bags contain (.*)");
            Regex ruleContents = new Regex("(\\d+|no) (.*?) bag");

            /* first we make each rule and add to the dictionary. then we will process each rules contents */
            foreach (string rule in baggageRules)
            {
                Match match = ruleStart.Match(rule);
                ruleMap.Add(match.Groups[1].Value, new BaggageRule()
                {
                    Type = match.Groups[1].Value, ContentString = match.Groups[2].Value
                });
            }

            foreach (BaggageRule rule in ruleMap.Values)
            {
                MatchCollection matches = ruleContents.Matches(rule.ContentString);
                foreach (Match match in matches)
                {
                    string amount = match.Groups[1].Value;
                    string type   = match.Groups[2].Value;
                    if (amount != "no")
                    {
                        BaggageRule containedType = ruleMap[type];
                        rule.Contents.Add(ruleMap[type], int.Parse(amount));
                        containedType.ContainedBy.Add(rule);
                    }
                }
            }
            Console.WriteLine($"Part 1 : {Part1()}");
            Console.WriteLine($"Part 2 : {Part2()}");
        }