From 8863f62fa81992915e10577751e6a7ab8f5a7c2c Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Wed, 2 Dec 2020 00:58:54 +0100 Subject: [PATCH] Project files and Day01. --- .../AdventOfCode2020.Tests.csproj | 20 ++ AdventOfCode2020.Tests/Day01_Tests.cs | 50 +++++ AdventOfCode2020.sln | 31 +++ AdventOfCode2020/AdventOfCode2020.csproj | 14 ++ AdventOfCode2020/Day01.cs | 89 ++++++++ AdventOfCode2020/IDay.cs | 8 + AdventOfCode2020/Program.cs | 46 ++++ AdventOfCode2020/inputs/Day01.txt | 200 ++++++++++++++++++ 8 files changed, 458 insertions(+) create mode 100644 AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj create mode 100644 AdventOfCode2020.Tests/Day01_Tests.cs create mode 100644 AdventOfCode2020.sln create mode 100644 AdventOfCode2020/AdventOfCode2020.csproj create mode 100644 AdventOfCode2020/Day01.cs create mode 100644 AdventOfCode2020/IDay.cs create mode 100644 AdventOfCode2020/Program.cs create mode 100644 AdventOfCode2020/inputs/Day01.txt diff --git a/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj b/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj new file mode 100644 index 0000000..4c20deb --- /dev/null +++ b/AdventOfCode2020.Tests/AdventOfCode2020.Tests.csproj @@ -0,0 +1,20 @@ + + + + net5.0 + + false + + + + + + + + + + + + + + diff --git a/AdventOfCode2020.Tests/Day01_Tests.cs b/AdventOfCode2020.Tests/Day01_Tests.cs new file mode 100644 index 0000000..0e0dd0c --- /dev/null +++ b/AdventOfCode2020.Tests/Day01_Tests.cs @@ -0,0 +1,50 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AdventOfCode2020.Tests +{ + [TestClass()] + public class Day01_Tests + { + #region ResolvePart1 + + [TestMethod()] + public void ResolvePart1__Example() + { + Day01 day01 = new Day01(); + + string result = day01.ResolvePart1(new string[] { + "1721", + "979", + "366", + "299", + "675", + "1456", + }); + + Assert.AreEqual("514579", result); + } + + #endregion ResolvePart1 + + #region ResolvePart2 + + [TestMethod()] + public void ResolvePart2__Example() + { + Day01 day01 = new Day01(); + + string result = day01.ResolvePart2(new string[] { + "1721", + "979", + "366", + "299", + "675", + "1456", + }); + + Assert.AreEqual("241861950", result); + } + + #endregion ResolvePart2 + } +} \ No newline at end of file diff --git a/AdventOfCode2020.sln b/AdventOfCode2020.sln new file mode 100644 index 0000000..24b5fc3 --- /dev/null +++ b/AdventOfCode2020.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30711.63 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode2020", "AdventOfCode2020\AdventOfCode2020.csproj", "{CD1829F6-166D-4F78-AB95-C58916F42494}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2020.Tests", "AdventOfCode2020.Tests\AdventOfCode2020.Tests.csproj", "{2B079D7D-08D4-4FFB-A000-858FFDD945FA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CD1829F6-166D-4F78-AB95-C58916F42494}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD1829F6-166D-4F78-AB95-C58916F42494}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD1829F6-166D-4F78-AB95-C58916F42494}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD1829F6-166D-4F78-AB95-C58916F42494}.Release|Any CPU.Build.0 = Release|Any CPU + {2B079D7D-08D4-4FFB-A000-858FFDD945FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B079D7D-08D4-4FFB-A000-858FFDD945FA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B079D7D-08D4-4FFB-A000-858FFDD945FA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B079D7D-08D4-4FFB-A000-858FFDD945FA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4BAE59D7-7AFF-42B6-A391-CD22CD443B03} + EndGlobalSection +EndGlobal diff --git a/AdventOfCode2020/AdventOfCode2020.csproj b/AdventOfCode2020/AdventOfCode2020.csproj new file mode 100644 index 0000000..cb46c89 --- /dev/null +++ b/AdventOfCode2020/AdventOfCode2020.csproj @@ -0,0 +1,14 @@ + + + + Exe + net5.0 + + + + + PreserveNewest + + + + diff --git a/AdventOfCode2020/Day01.cs b/AdventOfCode2020/Day01.cs new file mode 100644 index 0000000..33177c0 --- /dev/null +++ b/AdventOfCode2020/Day01.cs @@ -0,0 +1,89 @@ +using System; + +namespace AdventOfCode2020 +{ + /* +--- Day 1: Report Repair --- + +After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you. + +The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room. + +To save your vacation, you need to get all fifty stars by December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! + +Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. + +Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. + +For example, suppose your expense report contained the following: + +1721 +979 +366 +299 +675 +1456 + +In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. + +Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? + + +--- Part Two --- + +The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria. + +Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950. + +In your expense report, what is the product of the three entries that sum to 2020? + + + + */ + public class Day01 : IDay + { + public string ResolvePart1(string[] inputs) + { + int result = -1; + for (int i = 0; i < (inputs.Length - 1) && result < 0; i++) + { + for (int j = i + 1; j < inputs.Length && result < 0; j++) + { + if (string.IsNullOrEmpty(inputs[i]) || string.IsNullOrEmpty(inputs[j])) { continue; } + int numI = Convert.ToInt32(inputs[i]); + int numJ = Convert.ToInt32(inputs[j]); + if ((numI + numJ) == 2020) + { + result = numI * numJ; + } + } + } + return result.ToString(); + } + + public string ResolvePart2(string[] inputs) + { + long result = -1; + for (int i = 0; i < (inputs.Length - 2) && result < 0; i++) + { + for (int j = i + 1; j < (inputs.Length - 1) && result < 0; j++) + { + for (int k = j + 1; k < inputs.Length && result < 0; k++) + { + if (string.IsNullOrEmpty(inputs[i]) || string.IsNullOrEmpty(inputs[j]) || string.IsNullOrEmpty(inputs[k])) { continue; } + long numI = Convert.ToInt64(inputs[i]); + long numJ = Convert.ToInt64(inputs[j]); + long numK = Convert.ToInt64(inputs[k]); + if ((numI + numJ + numK) == 2020) + { + result = numI * numJ * numK; + } + } + } + } + return result.ToString(); + } + } +} diff --git a/AdventOfCode2020/IDay.cs b/AdventOfCode2020/IDay.cs new file mode 100644 index 0000000..3c2f99f --- /dev/null +++ b/AdventOfCode2020/IDay.cs @@ -0,0 +1,8 @@ +namespace AdventOfCode2020 +{ + public interface IDay + { + string ResolvePart1(string[] inputs); + string ResolvePart2(string[] inputs); + } +} diff --git a/AdventOfCode2020/Program.cs b/AdventOfCode2020/Program.cs new file mode 100644 index 0000000..45381de --- /dev/null +++ b/AdventOfCode2020/Program.cs @@ -0,0 +1,46 @@ +using System; +using System.IO; + +namespace AdventOfCode2020 +{ + class Program + { + static void Main(string[] args) + { + int currentDayNumber = 1; + IDay currentDay = null; + + switch (currentDayNumber) + { + case 1: currentDay = new Day01(); break; + } + + Console.WriteLine(string.Format("Day {0:00}", currentDayNumber)); + Console.WriteLine("------"); + Console.WriteLine(); + string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber)); + try + { + string resultPart1 = currentDay.ResolvePart1(linesDay); + Console.WriteLine("Day{1:00} Result Part1: {0}", resultPart1, currentDayNumber); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + Console.WriteLine(ex.StackTrace); + } + try + { + string resultPart2 = currentDay.ResolvePart2(linesDay); + Console.WriteLine("Day{1:00} Result Part2: {0}", resultPart2, currentDayNumber); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + Console.WriteLine(ex.StackTrace); + } + + Console.Read(); + } + } +} diff --git a/AdventOfCode2020/inputs/Day01.txt b/AdventOfCode2020/inputs/Day01.txt new file mode 100644 index 0000000..99a5c87 --- /dev/null +++ b/AdventOfCode2020/inputs/Day01.txt @@ -0,0 +1,200 @@ +1293 +1207 +1623 +1675 +1842 +1410 +85 +1108 +557 +1217 +1506 +1956 +1579 +1614 +1360 +1544 +1946 +1666 +1972 +1814 +1699 +1778 +1529 +2002 +1768 +1173 +1407 +1201 +1264 +1739 +1774 +1951 +1980 +1428 +1381 +1714 +884 +1939 +1295 +1694 +1168 +1971 +1352 +1462 +1828 +1402 +1433 +1542 +1144 +1331 +1427 +1261 +1663 +1820 +1570 +1874 +1486 +1613 +1769 +1721 +1753 +1142 +1677 +2010 +1640 +1465 +1171 +534 +1790 +2005 +1604 +1891 +1247 +1281 +1867 +1403 +2004 +1668 +1416 +2001 +1359 +686 +1965 +1728 +1551 +1565 +1128 +1832 +1757 +1350 +1808 +1711 +1799 +1590 +1989 +1547 +1140 +1905 +1368 +1179 +1902 +1473 +1908 +1859 +1257 +1394 +1244 +1800 +1695 +1731 +1474 +1781 +1885 +1154 +1990 +1929 +1193 +1302 +1831 +1226 +1418 +1400 +1435 +1645 +1655 +1843 +1227 +1481 +1754 +1290 +1685 +1498 +71 +1286 +1137 +1288 +1758 +1987 +1471 +1839 +1545 +1682 +1615 +1475 +1849 +1985 +1568 +1795 +1184 +1863 +1362 +1271 +1802 +1944 +1821 +1880 +1788 +1733 +1150 +1314 +1727 +1434 +1833 +1312 +1457 +160 +1629 +1967 +1505 +1239 +1266 +1838 +1687 +1630 +1591 +1893 +1450 +1234 +1755 +1523 +1533 +1499 +1865 +1725 +1444 +1517 +1167 +1738 +1519 +1263 +1901 +1627 +1644 +1771 +1812 +1270 +1497 +1707 +1708 +1396