Project files and Day01.

This commit is contained in:
2020-12-02 00:58:54 +01:00
parent 57de59140f
commit 8863f62fa8
8 changed files with 458 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Update="inputs\Day01.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

89
AdventOfCode2020/Day01.cs Normal file
View File

@@ -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();
}
}
}

8
AdventOfCode2020/IDay.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace AdventOfCode2020
{
public interface IDay
{
string ResolvePart1(string[] inputs);
string ResolvePart2(string[] inputs);
}
}

View File

@@ -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();
}
}
}

View File

@@ -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