Пример #1
0
        static Graph LoadBags(string input)
        {
            var graph = new Graph();

            foreach (var line in FileIterator.Lines(input))
            {
                var groups  = line.Groups("^(.+) bags contain (.+)\\.$");
                var bagType = groups[0];
                var bagNode = graph.GetOrCreate(bagType, () => (new(), new()));

                if (groups[1] != "no other bags")
                {
                    var children = groups[1].Split(',');
                    foreach (var child in children)
                    {
                        groups = child.Groups("(\\d+) (.+) bag");
                        bagNode.Item2.Add((int.Parse(groups[0]), groups[1]));

                        var childNode = graph.GetOrCreate(groups[1], () => (new(), new()));
                        childNode.Item1.Add(bagType);
                    }
                }
            }

            return(graph);
        }