コード例 #1
0
        private bool BagTraversalRec(BagNode currentNode, string bagName)
        {
            if (!currentNode.ContainedBags.Any())
            {
                return(false);
            }

            return(currentNode.ContainedBags.Any(b => b.BagName == bagName) ||
                   currentNode.ContainedBags.Any(b => BagTraversalRec(nodeDict[b.BagName], bagName)));
        }
コード例 #2
0
        public static BagNode ParseBagInput(string inputLine)
        {
            var retNode = new BagNode();

            var splitInput = inputLine.Split(' ');

            retNode.BagName = splitInput[0] + " " + splitInput[1];
            var inputIndex = 4;

            while (inputIndex <= splitInput.Length - 4 && splitInput[inputIndex] != "no")
            {
                retNode.ContainedBags.Add(new BagNode
                {
                    BagCount = Convert.ToInt32(splitInput[inputIndex]),
                    BagName  = splitInput[inputIndex + 1] + " " + splitInput[inputIndex + 2]
                });

                inputIndex = inputIndex + 4;
            }

            return(retNode);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                Console.WriteLine("Provide input file name.");
                return;
            }

            var graph = new BagGraph();

            using (StreamReader fileStream = new StreamReader(new FileInfo(args[0]).OpenRead()))
            {
                while (fileStream.Peek() >= 0)
                {
                    var node = BagNode.ParseBagInput(fileStream.ReadLine());
                    graph.AddNode(node);
                }
            }

            Console.WriteLine(graph.BagTraversal("shiny gold"));
            Console.WriteLine(graph.ContainedBagCount("shiny gold"));
        }
コード例 #4
0
 public void AddNode(BagNode node)
 {
     nodeDict.Add(node.BagName, node);
 }