public FactItem(string input) { if (string.IsNullOrEmpty(input) || !input.EndsWith('.') || !input.Contains("bags contain")) { throw new ArgumentNullException($"Invalid input: {input}"); } var splitInput = input.Split("bags contain"); What = splitInput[0].Trim(); var splitContains = splitInput[1]; if (!splitContains.Contains("no other bags.")) { var splitWhat = splitContains.Split(',').Select(item => item.Trim().Replace(".", "")); foreach (var item in splitWhat) { var splitItem = item.Split(" "); if (splitItem.Length != 4) { throw new ArgumentException($"Invalid argument: {item}", nameof(input)); } Contains.Add($"{splitItem[1]} {splitItem[2]}".Trim(), int.Parse(splitItem[0].Trim())); } } }
public void Deepen() { if (Deepened) { return; } var copy = new List <BagCounter>(); foreach (var cpy in Contains) { copy.Add(new BagCounter() { Count = cpy.Count, Name = cpy.Name }); } foreach (var bag in copy) { var foundBag = Bags.FirstOrDefault(x => x.BagName == bag.Name); if (foundBag == null) { continue; } if (!foundBag.Deepened) { foundBag.Deepen(); } foreach (var toAdd in foundBag.Contains) { if (Contains.Any(x => x.Name == toAdd.Name)) { var index = Contains.FindIndex(x => x.Name == toAdd.Name); Contains[index].Count += toAdd.Count * bag.Count; } else { Contains.Add(new BagCounter() { Name = toAdd.Name, Count = toAdd.Count * bag.Count }); } } } Deepened = true; }
public Bag(string rule) { var tokens = rule.Split(" bags contain "); BagName = tokens[0]; if (tokens[1] != "no other bags.") { var bags = tokens[1].Split(','); foreach (var bag in bags) { var bagTokens = bag.Trim().Split(' ').ToList(); var count = int.Parse(bagTokens[0]); bagTokens.RemoveAt(0); bagTokens.RemoveAt(bagTokens.Count - 1); Contains.Add(new BagCounter() { Name = string.Join(' ', bagTokens), Count = count }); } } }
public DBQuery WhereRecordContainsValue(string column, string value) { Contains.Add(column, value); return(this); }