예제 #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);
 }