From 147ee33b30d12cce8a1285f456a0ec9d9b31bb1b Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 1 Dec 2020 18:24:31 +0100 Subject: Added files for first day I wrote this in school, sorry for no documentation etc --- 2020/1/Makefile | 10 +++ 2020/1/input | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2020/1/run.cs | 59 +++++++++++++++++ 3 files changed, 269 insertions(+) create mode 100644 2020/1/Makefile create mode 100644 2020/1/input create mode 100644 2020/1/run.cs (limited to '2020/1') diff --git a/2020/1/Makefile b/2020/1/Makefile new file mode 100644 index 0000000..c678ba8 --- /dev/null +++ b/2020/1/Makefile @@ -0,0 +1,10 @@ +.PHONY: run.cs + +run.exe: run.cs + @mcs $+ + +clean: + @rm -f run.exe + +run: run.exe + @mono run.exe diff --git a/2020/1/input b/2020/1/input new file mode 100644 index 0000000..f522f3f --- /dev/null +++ b/2020/1/input @@ -0,0 +1,200 @@ +1227 +1065 +329 +1063 +1889 +1700 +1805 +1373 +389 +1263 +1276 +1136 +1652 +1981 +1406 +1249 +1197 +1379 +1050 +1791 +1703 +2001 +1842 +1707 +1486 +1204 +1821 +1807 +1712 +1871 +1599 +1390 +1219 +1612 +1980 +1857 +1511 +1702 +1455 +1303 +1052 +1754 +1545 +1488 +1848 +1236 +1549 +1887 +1970 +1123 +1686 +1404 +1688 +1106 +1296 +401 +1829 +1693 +1389 +1957 +914 +1176 +1348 +1275 +1624 +1401 +1045 +1396 +1352 +1569 +1060 +1235 +1679 +1503 +1340 +1872 +1410 +1077 +958 +1681 +1189 +1466 +1087 +1852 +1293 +1139 +1300 +1323 +661 +1388 +1983 +1325 +1112 +1774 +1858 +1785 +1616 +1255 +1198 +1354 +1124 +1834 +1417 +1918 +1496 +33 +1150 +1861 +1172 +2006 +1199 +1558 +1919 +1620 +1613 +1710 +1477 +1592 +1709 +1909 +1670 +1922 +1840 +1768 +1982 +1193 +1736 +1877 +1770 +1191 +1433 +1072 +1148 +1225 +1147 +1171 +1424 +1913 +1228 +1339 +1814 +1504 +1251 +1240 +1272 +1500 +1927 +1428 +1641 +1453 +1729 +1976 +1808 +1180 +1024 +1108 +1085 +1669 +1636 +1005 +1520 +1929 +1626 +1551 +1234 +1988 +1256 +1524 +1571 +1506 +1977 +1749 +1408 +1540 +1934 +1810 +1328 +1910 +1478 +1600 +1699 +1413 +1446 +1798 +1013 +1998 +1661 +1058 +1051 +1220 +1447 +1675 +1912 +1668 +1932 +1962 +1055 +1757 +1116 +1090 diff --git a/2020/1/run.cs b/2020/1/run.cs new file mode 100644 index 0000000..fb2f6ae --- /dev/null +++ b/2020/1/run.cs @@ -0,0 +1,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]); + } + } +} -- cgit v1.2.3