public List <LuggageRule> ParseRules(List <string> rules)
    {
        var luggageRules = new List <LuggageRule>();

        foreach (var line in rules)
        {
            var parts           = line.Split("bags contain");
            var color           = parts[0].Trim();
            var luggageRule     = new LuggageRule(color);
            var requiredContent = parts[1].Split(",");
            foreach (var requiredBag in requiredContent)
            {
                if (requiredBag.Contains("no other bags"))
                {
                    continue;
                }
                var bagParts            = requiredBag.Trim(' ').Split(" ");
                var requiredBagQuantity = Int32.Parse(bagParts[0]);
                var bagColor            = bagParts[1] + " " + bagParts[2];

                var bagContent = new BagContent(bagColor, requiredBagQuantity);
                luggageRule.RequiredContent.Add(bagContent);
            }
            luggageRules.Add(luggageRule);
        }

        return(luggageRules);
    }
Esempio n. 2
0
        private static List <LuggageRule> GatherRules(IEnumerable <string> data)
        {
            var rules = new List <LuggageRule>();

            foreach (var line in data)
            {
                rules.Add(LuggageRule.Parse(line));
            }

            return(rules);
        }
Esempio n. 3
0
        private bool SearchForContainingBags(string bagColor, LuggageRule currentRule)
        {
            foreach (var bag in currentRule.InnerBags)
            {
                if (bag.Color == bagColor)
                {
                    return(true);
                }
                return(SearchForContainingBags(bagColor, _luggageRules.Where(r => r.OuterColor == bag.Color).First()));
            }

            return(false);
        }
Esempio n. 4
0
        private void MapInputToLuggageRules(List <string> rawInput)
        {
            _luggageRules = new List <LuggageRule>();

            foreach (var rawRule in rawInput)
            {
                var splitRule = rawRule.Split("contain");

                var newRule = new LuggageRule()
                {
                    OuterColor = splitRule[0].Replace("bags", "").Trim(),
                    InnerBags  = CreateInnerBagsFromRawRule(splitRule[1])
                };
                _luggageRules.Add(newRule);
            }
        }
Esempio n. 5
0
    private int CountAllRequiredBagsToEquip(LuggageRule bagRule, List <LuggageRule> rules)
    {
        if (bagRule.RequiredContent.Count == 0)
        {
            return(0);
        }

        var counter = 0;

        foreach (var requiredBag in bagRule.RequiredContent)
        {
            counter += requiredBag.RequiredQuantity;
        }

        foreach (var requiredBag in bagRule.RequiredContent)
        {
            var rule = rules.First(r => r.BagColor.Equals(requiredBag.BagColor));
            counter += (CountAllRequiredBagsToEquip(rule, rules) * requiredBag.RequiredQuantity);
        }

        return(counter);
    }