aboutsummaryrefslogtreecommitdiff
path: root/2020/01/solve.cs
blob: fb2f6ae73e1716a51c2af575f330b3f0813a2290 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.IO;
using System.Linq;

namespace AOC
{
    internal class Run
    {
        private static int[] Double(int[] arr, int low, int result)
        {
            int high = arr.Length - 1;

            int sum = arr[low] + arr[high];
            while (low < high && sum != result)
            {
                if (sum < result)
                    low++;
                else
                    high--;

                sum = arr[low] + arr[high];
            }

            if (low == high)
            {
                return new[] {0, 0};
            }

            return new[] {arr[low], arr[high]};
        }

        private static int[] Triple(int[] arr, int result)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                int remainder = result - arr[i];
                int[] d = Double(arr, i, remainder);
                if (d.Sum() != 0)
                {
                    return new[] {arr[i], d[0], d[1]};
                }
            }

            return new[] {0, 0};
        }

        public static void Main(string[] args)
        {
            string text = File.ReadAllText("input");
            string[] numsStr = text.Split('\n');
            numsStr[numsStr.Length - 1] = "2020"; // Some weird newline character ig
            int[] nums = Array.ConvertAll(numsStr, int.Parse).OrderBy(i => i).ToArray();
            int[] d = Double(nums, 0, 2020);
            int[] t = Triple(nums, 2020);
            Console.WriteLine(d[0] * d[1]);
            Console.WriteLine(t[0] * t[1] * t[2]);
        }
    }
}