Day02 part 1

This commit is contained in:
2018-12-08 00:22:44 +01:00
parent b8a42fd95c
commit d6866863ea
6 changed files with 100 additions and 1 deletions

View File

@@ -57,6 +57,7 @@
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="Day01_Tests.cs" /> <Compile Include="Day01_Tests.cs" />
<Compile Include="Day02_Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AdventOfCode2017.Tests
{
[TestClass()]
public class Day02_Tests
{
[TestMethod()]
public void ResolvePart1__Test()
{
Day02 day02 = new Day02();
string result = day02.ResolvePart1(new string[] {
"5 1 9 5",
"7 5 3",
"2 4 6 8",
});
Assert.AreEqual("18", result);
}
}
}

View File

@@ -44,6 +44,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Day01.cs" /> <Compile Include="Day01.cs" />
<Compile Include="Day02.cs" />
<Compile Include="IDay.cs" /> <Compile Include="IDay.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@@ -52,6 +53,9 @@
<Content Include="inputs\Day01.txt"> <Content Include="inputs\Day01.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="inputs\Day02.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

55
AdventOfCode2017/Day02.cs Normal file
View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2017
{
/*
--- Day 2: Corruption Checksum ---
As you walk through the door, a glowing humanoid shape yells in your direction. "You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we'll have to display an hourglass cursor!"
The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.
For example, given the following spreadsheet:
5 1 9 5
7 5 3
2 4 6 8
The first row's largest and smallest values are 9 and 1, and their difference is 8.
The second row's largest and smallest values are 7 and 3, and their difference is 4.
The third row's difference is 6.
In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
What is the checksum for the spreadsheet in your puzzle input?
*/
public class Day02 : IDay
{
public string ResolvePart1(string[] inputs)
{
int checksum = 0;
foreach(string input in inputs)
{
int[] row = input
.Split(new string[] { " ", " " }, StringSplitOptions.RemoveEmptyEntries)
.Select(cell => Convert.ToInt32(cell))
.ToArray();
int max = row.Max();
int min = row.Min();
checksum += (max - min);
}
return checksum.ToString();
}
public string ResolvePart2(string[] inputs)
{
return null;
}
}
}

View File

@@ -7,12 +7,13 @@ namespace AdventOfCode2017
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
int currentDayNumber = 1; int currentDayNumber = 2;
IDay currentDay = null; IDay currentDay = null;
switch (currentDayNumber) switch (currentDayNumber)
{ {
case 1: currentDay = new Day01(); break; case 1: currentDay = new Day01(); break;
case 2: currentDay = new Day02(); break;
} }
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber)); string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));

View File

@@ -0,0 +1,16 @@
1919 2959 82 507 3219 239 3494 1440 3107 259 3544 683 207 562 276 2963
587 878 229 2465 2575 1367 2017 154 152 157 2420 2480 138 2512 2605 876
744 6916 1853 1044 2831 4797 213 4874 187 6051 6086 7768 5571 6203 247 285
1210 1207 1130 116 1141 563 1056 155 227 1085 697 735 192 1236 1065 156
682 883 187 307 269 673 290 693 199 132 505 206 231 200 760 612
1520 95 1664 1256 685 1446 253 88 92 313 754 1402 734 716 342 107
146 1169 159 3045 163 3192 1543 312 161 3504 3346 3231 771 3430 3355 3537
177 2129 3507 3635 2588 3735 3130 980 324 266 1130 3753 175 229 517 3893
4532 164 191 5169 4960 3349 3784 3130 5348 5036 2110 151 5356 193 1380 3580
2544 3199 3284 3009 3400 953 3344 3513 102 1532 161 143 2172 2845 136 2092
194 5189 3610 4019 210 256 5178 4485 5815 5329 5457 248 5204 4863 5880 3754
3140 4431 4534 4782 3043 209 216 5209 174 161 3313 5046 1160 160 4036 111
2533 140 4383 1581 139 141 2151 2104 2753 4524 4712 866 3338 2189 116 4677
1240 45 254 1008 1186 306 633 1232 1457 808 248 1166 775 1418 1175 287
851 132 939 1563 539 1351 1147 117 1484 100 123 490 152 798 1476 543
1158 2832 697 113 121 397 1508 118 2181 2122 809 2917 134 2824 3154 2791