Exemplo n.º 1
0
        /// <summary>
        /// The main method that is called outside this class that will solve the puzzle
        /// and return the answer
        /// </summary>
        /// <returns>The answer to the puzzle</returns>
        public int solvePuzzle()
        {
            // load the data from the PuzzleData.txt
            string puzzleData = this.LoadPuzzleDataIntoMemory();

            // create an instance of bagParser which will parse the puzzleData text
            Bags.BagParser bagParser = new Bags.BagParser();
            // parze the puzzle data text
            System.Collections.Hashtable bags = bagParser.parseBagText(puzzleData);
            // find the "shiny gold" bag
            Bags.Bag aBag = (Bags.Bag)bags["shiny gold"];

            // count how many unique parent bags the "shiny gold" bag has
            this.findParentBags(aBag);
            // return the unique number of parent bags the "shiny gold" bag has
            return(this._uniqueParentBags.Count);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Recurive function that counts the number of child bags within a bag
        /// </summary>
        /// <param name="aBag">Recursivly count all child bags</param>
        /// <param name="NoOfParentBags">the number of parent bags</param>
        /// <returns></returns>
        private int FindAllChildBags_Test(Bags.Bag aBag, int NoOfParentBags)
        {
            int bagCount = 0;

            // loop through each child bag within this bag
            foreach (Bags.ChildBag childBag in aBag.bagsWithinThisBag)
            {
                // caculate the number of child bags there are based on the number of parent bags
                bagCount += childBag.NumberOfThisKindOfBag * NoOfParentBags;


                // count the number of childdren this child bag has
                bagCount += this.FindAllChildBags_Test(childBag.bag, childBag.NumberOfThisKindOfBag * NoOfParentBags);
            }


            return(bagCount);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The main method that is called outside this class that will solve the puzzle
        /// and return the answer
        /// </summary>
        /// <returns>The answer to the puzzle</returns>
        public int solvePuzzle()
        {
            // load the data from the PuzzleData.txt
            string puzzleData = this.LoadPuzzleDataIntoMemory();

            // create an instance of bagParser which will parse the puzzleData text
            Bags.BagParser bagParser = new Bags.BagParser();
            // parze the puzzle data text
            System.Collections.Hashtable bags = bagParser.parseBagText(puzzleData);
            // find the "shiny gold" bag
            Bags.Bag aBag = (Bags.Bag)bags["shiny gold"];

            int NoParentBags = 1;
            // count the number of child bags within aBag
            int answer = this.FindAllChildBags_Test(aBag, NoParentBags);

            return(answer);
        }
Exemplo n.º 4
0
        /// <summary>
        /// find all the parent bags and check track of the ones we find within the verable _quniqueParentBags
        /// </summary>
        /// <param name="aBag">the bag to start checking from</param>
        private void findParentBags(Bags.Bag aBag)
        {
            // go through each bags prents
            foreach (System.Collections.DictionaryEntry entry in aBag.parentBags)
            {
                // get the current bags parent we are looking at in the loop
                Bags.Bag parentBag = (Bags.Bag)entry.Value;

                // keep track of the parent bags we have found and add them to the list
                // but only add them if its the first time we have come accross this parent bag
                // Dont add if we have come accross it before within the recursive findParentBags function
                if (_uniqueParentBags.ContainsKey(parentBag.bagColor) == false)
                {
                    _uniqueParentBags.Add(parentBag.bagColor, parentBag);
                }

                // check this parent to see if it has any parent bags
                findParentBags(parentBag);
            }

            return;
        }