Пример #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 puzzle data into memory
            string puzzleData = this.LoadPuzzleDataIntoMemory();

            // split puzzel data into each line
            string[] puzzleDataSplitIntoLines = puzzleData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
            // a place to hole all the seatID's when we have computed them
            List <int> seatIDList = new List <int>();

            // go through each line (bording pass)
            foreach (string aBordingPassAsString in puzzleDataSplitIntoLines)
            {
                BoardingPass.BoardingPass aBordingPass = new BoardingPass.BoardingPass();
                // pass in the bording pass which will parse the data to work out which row, colum and seatID the person has
                aBordingPass.parseBoardingPass(aBordingPassAsString);
                // add this bording passes seat ID to the list
                seatIDList.Add(aBordingPass.seatID);
            }
            // sort the seat ID's from smallest to biggiest
            seatIDList.Sort();


            // return the last seat id in the list which will be the biggest number
            return(seatIDList[seatIDList.Count - 1]);
        }
Пример #2
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 puzzle data into memory
            string puzzleData = this.LoadPuzzleDataIntoMemory();

            // split puzzel data into each line
            string[] puzzleDataSplitIntoLines = puzzleData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
            // a place to hole all the seatID's when we have computed them
            List <int> seatIDList = new List <int>();

            // go through each line (bording pass)
            foreach (string aBordingPassAsString in puzzleDataSplitIntoLines)
            {
                BoardingPass.BoardingPass aBordingPass = new BoardingPass.BoardingPass();
                // pass in the bording pass which will parse the data to work out which row, colum and seatID the person has
                aBordingPass.parseBoardingPass(aBordingPassAsString);
                // add this bording passes seat ID to the list
                seatIDList.Add(aBordingPass.seatID);
            }
            // sort the seat ID's from smallest to biggiest
            seatIDList.Sort();
            // this will be the answer to part 2 of the puzzle
            int missingSeatID = -1;

            // go through each seatid in the list seatIDList
            for (int i = 0; i < seatIDList.Count - 1; i++)
            {
                // look to see if the next seat in the list is one number bigger
                // to the current seatid we are looking at in the list.
                // if its not one bigger, we have found the answer to part 2 of the puzzle
                if (seatIDList[i + 1] != seatIDList[i] + 1)
                {
                    //int thisSeatID = seatIDList[i];
                    //int nextSeatID = seatIDList[i + 1];

                    missingSeatID = seatIDList[i] + 1;
                    break;
                }
            }

            // the seat id that was missing in the seatIDList
            return(missingSeatID);
        }