Example #1
0
        public static int FirstPart(string filename)
        {
            var lines       = ReadInputs.ReadAllInts(ReadInputs.GetFullPath(filename)).OrderBy(l => l).ToList();
            var numOfOnes   = 0;
            var numOfThrees = 0;

            var currentVoltage = 0;
            var myVoltage      = lines[lines.Count - 1] + 3;

            foreach (var line in lines)
            {
                if (line - currentVoltage == 1)
                {
                    numOfOnes++;
                }
                else if (line - currentVoltage == 3)
                {
                    numOfThrees++;
                }

                currentVoltage = line;
            }

            return(numOfOnes * (numOfThrees + 1));
        }
Example #2
0
        /// <summary>
        /// Method to be run to ge the 2nd part of the solution.
        /// Find three numbers in list that give a sum of 2020
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static int SecondPart(string filename)
        {
            var numbers = ReadInputs.ReadAllInts(filename);

            for (int i = 0; i < numbers.Count; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (numbers[i] + numbers[j] > 2020)
                    {
                        continue;                                                   // if two numbers are greater than 2020, no sense trying out the 3rd.
                    }
                    for (int k = 0; k < j; k++)
                    {
                        if (numbers[i] + numbers[j] + numbers[k] == 2020)
                        {
                            return(numbers[i] * numbers[j] * numbers[k]);
                        }
                    }
                }
            }

            throw new Exception("No sequence found.");
        }