Exemplo n.º 1
0
        /// <summary>
        /// Find N numbers with expected sum
        /// </summary>
        /// <param name="sum">Expected sum</param>
        /// <param name="count">Number of elements to find</param>
        /// <returns>N numbers whose sum is equal to expected sum, ordered by smaller ones first</returns>
        public int[] FindNWithSum(int sum, int count = 2)
        {
            if (count < 1)
            {
                throw new ArgumentException("Count must be at least 1", nameof(count));
            }

            if (SortedNumbers.Count < count)
            {
                return(null);
            }

            if (count == 1)
            {
                return(SortedNumbers.Contains(sum) ? new[] { sum } : null);
            }

            var calculatorWithoutFirst = new ExpenseCalculator(SortedNumbers.Skip(1));

            var setIfFirstIsThere = calculatorWithoutFirst.FindNWithSum(sum - SortedNumbers.First(), count - 1);

            if (setIfFirstIsThere != null)
            {
                // first element should be part of result
                var result = new List <int> {
                    SortedNumbers.First()
                };
                result.AddRange(setIfFirstIsThere);
                return(result.ToArray());
            }

            // first element should not be part of result
            return(calculatorWithoutFirst.FindNWithSum(sum, count));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var expenses          = File.ReadAllLines("input.txt").Select(int.Parse);
            var expenseCalculator = new ExpenseCalculator();

            // Part 1
            Console.WriteLine(expenseCalculator.Magic(expenses, 2));

            // Part 2
            Console.WriteLine(expenseCalculator.Magic(expenses, 3));
        }
Exemplo n.º 3
0
 public void LoadInput()
 {
     ExpenseCalculator = new ExpenseCalculator(InputGetter.ReadInputLinesInt(1));
 }