Refactor to use interface IDay

This commit is contained in:
2018-12-07 01:30:45 +01:00
parent 2760f9f9de
commit 4cffe19c3a
11 changed files with 56 additions and 86 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AdventOfCode2018.Tests namespace AdventOfCode2018.Tests
{ {
@@ -27,14 +28,15 @@ namespace AdventOfCode2018.Tests
{ {
Day06 day06 = new Day06(); Day06 day06 = new Day06();
string result = day06.ResolvePart2(new string[] { List<ChronoPoint> points = day06.InputsToPoints(new string[] {
"1, 1", "1, 1",
"1, 6", "1, 6",
"8, 3", "8, 3",
"3, 4", "3, 4",
"5, 5", "5, 5",
"8, 9", "8, 9",
}, 32); });
int result = day06.AreaInThresold(points, 32);
Assert.AreEqual("16", result); Assert.AreEqual("16", result);
} }

View File

@@ -49,6 +49,7 @@
<Compile Include="Day04.cs" /> <Compile Include="Day04.cs" />
<Compile Include="Day05.cs" /> <Compile Include="Day05.cs" />
<Compile Include="Day06.cs" /> <Compile Include="Day06.cs" />
<Compile Include="IDay.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

View File

@@ -59,7 +59,7 @@ namespace AdventOfCode2018
*/ */
public class Day01 public class Day01 : IDay
{ {
public string ResolvePart1(string[] inputs) public string ResolvePart1(string[] inputs)
{ {

View File

@@ -51,7 +51,7 @@ namespace AdventOfCode2018
*/ */
public class Day02 public class Day02 : IDay
{ {
private int CountOccurrencesOfLetter(string text, char letter) private int CountOccurrencesOfLetter(string text, char letter)
{ {

View File

@@ -61,7 +61,7 @@ namespace AdventOfCode2018
*/ */
public class Day03 public class Day03 : IDay
{ {
public string ResolvePart1(string[] inputs) public string ResolvePart1(string[] inputs)
{ {

View File

@@ -67,7 +67,7 @@ namespace AdventOfCode2018
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.) What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.)
*/ */
public class Day04 public class Day04 : IDay
{ {
public string ResolvePart1(string[] inputs) public string ResolvePart1(string[] inputs)
{ {

View File

@@ -50,7 +50,7 @@ namespace AdventOfCode2018
*/ */
public class Day05 public class Day05 : IDay
{ {
public string ReducePolymer(string polymer) public string ReducePolymer(string polymer)
{ {
@@ -88,9 +88,9 @@ namespace AdventOfCode2018
return polymer; return polymer;
} }
public string ResolvePart1(string input) public string ResolvePart1(string[] inputs)
{ {
string reducedPolymer = FullyReducePolymer(input); string reducedPolymer = FullyReducePolymer(inputs[0]);
return reducedPolymer.Length.ToString(); return reducedPolymer.Length.ToString();
} }
@@ -108,8 +108,9 @@ namespace AdventOfCode2018
return sb.ToString(); return sb.ToString();
} }
public string ResolvePart2(string input) public string ResolvePart2(string[] inputs)
{ {
string input = inputs[0];
List<char> allUnitTypes = input.Select(c => char.ToLower(c)).Distinct().ToList(); List<char> allUnitTypes = input.Select(c => char.ToLower(c)).Distinct().ToList();
int minPolymerLenght = int.MaxValue; int minPolymerLenght = int.MaxValue;

View File

@@ -95,14 +95,19 @@ namespace AdventOfCode2018
*/ */
public class Day06 public class Day06 : IDay
{ {
public string ResolvePart1(string[] inputs) public List<ChronoPoint> InputsToPoints(string[] inputs)
{ {
List<ChronoPoint> points = inputs return inputs
.Where(input => string.IsNullOrEmpty(input) == false) .Where(input => string.IsNullOrEmpty(input) == false)
.Select(input => ChronoPoint.FromString(input)) .Select(input => ChronoPoint.FromString(input))
.ToList(); .ToList();
}
public string ResolvePart1(string[] inputs)
{
List<ChronoPoint> points = InputsToPoints(inputs);
Dictionary<int, int> pointsAreas = new Dictionary<int, int>(); Dictionary<int, int> pointsAreas = new Dictionary<int, int>();
for (int i = 0; i < points.Count; i++) for (int i = 0; i < points.Count; i++)
{ {
@@ -159,13 +164,8 @@ namespace AdventOfCode2018
return maxArea.ToString(); return maxArea.ToString();
} }
public string ResolvePart2(string[] inputs, int distanceThresold) public int AreaInThresold(List<ChronoPoint> points, int distanceThresold)
{ {
List<ChronoPoint> points = inputs
.Where(input => string.IsNullOrEmpty(input) == false)
.Select(input => ChronoPoint.FromString(input))
.ToList();
int minX = points.Min(p => p.X) - 1; int minX = points.Min(p => p.X) - 1;
int maxX = points.Max(p => p.X) + 1; int maxX = points.Max(p => p.X) + 1;
int minY = points.Min(p => p.Y) - 1; int minY = points.Min(p => p.Y) - 1;
@@ -184,7 +184,14 @@ namespace AdventOfCode2018
if (distanceSum < distanceThresold) { areaInRange++; } if (distanceSum < distanceThresold) { areaInRange++; }
} }
} }
return areaInRange;
}
public string ResolvePart2(string[] inputs)
{
List<ChronoPoint> points = InputsToPoints(inputs);
int areaInRange = AreaInThresold(points, 10000);
return areaInRange.ToString(); return areaInRange.ToString();
} }
} }

8
AdventOfCode2018/IDay.cs Normal file
View File

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

View File

@@ -7,73 +7,24 @@ namespace AdventOfCode2018
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
int currentDay = 6; int currentDayNumber = 6;
IDay currentDay = null;
if (currentDay == 1) switch (currentDayNumber)
{ {
// Resolve Day01 case 1: currentDay = new Day01(); break;
Day01 day01 = new Day01(); case 2: currentDay = new Day02(); break;
string[] linesDay01 = File.ReadAllLines("inputs/Day01.txt"); case 3: currentDay = new Day03(); break;
string resultPart1 = day01.ResolvePart1(linesDay01); case 4: currentDay = new Day04(); break;
Console.WriteLine("Day01 Result Part1: {0}", resultPart1); case 5: currentDay = new Day05(); break;
string resultPart2 = day01.ResolvePart2(linesDay01); case 6: currentDay = new Day06(); break;
Console.WriteLine("Day01 Result Part2: {0}", resultPart2);
}
if (currentDay == 2)
{
// Resolve Day02
Day02 day02 = new Day02();
string[] linesDay02 = File.ReadAllLines("inputs/Day02.txt");
string resultPart1 = day02.ResolvePart1(linesDay02);
Console.WriteLine("Day02 Result Part1: {0}", resultPart1);
string resultPart2 = day02.ResolvePart2(linesDay02);
Console.WriteLine("Day02 Result Part2: {0}", resultPart2);
}
if (currentDay == 3)
{
// Resolve Day03
Day03 day03 = new Day03();
string[] linesDay03 = File.ReadAllLines("inputs/Day03.txt");
string resultPart1 = day03.ResolvePart1(linesDay03);
Console.WriteLine("Day03 Result Part1: {0}", resultPart1);
string resultPart2 = day03.ResolvePart2(linesDay03);
Console.WriteLine("Day03 Result Part2: {0}", resultPart2);
}
if (currentDay == 4)
{
// Resolve Day04
Day04 day04 = new Day04();
string[] linesDay04 = File.ReadAllLines("inputs/Day04.txt");
string resultPart1 = day04.ResolvePart1(linesDay04);
Console.WriteLine("Day04 Result Part1: {0}", resultPart1);
string resultPart2 = day04.ResolvePart2(linesDay04);
Console.WriteLine("Day04 Result Part2: {0}", resultPart2);
}
if (currentDay == 5)
{
// Resolve Day05
Day05 day05 = new Day05();
string dataDay05 = File.ReadAllText("inputs/Day05.txt");
string resultPart1 = day05.ResolvePart1(dataDay05);
Console.WriteLine("Day04 Result Part1: {0}", resultPart1);
string resultPart2 = day05.ResolvePart2(dataDay05);
Console.WriteLine("Day04 Result Part2: {0}", resultPart2);
}
if (currentDay == 6)
{
// Resolve Day06
Day06 day06 = new Day06();
string[] linesDay06 = File.ReadAllLines("inputs/Day06.txt");
string resultPart1 = day06.ResolvePart1(linesDay06);
Console.WriteLine("Day06 Result Part1: {0}", resultPart1);
string resultPart2 = day06.ResolvePart2(linesDay06, 10000);
Console.WriteLine("Day06 Result Part2: {0}", resultPart2);
} }
string[] linesDay = File.ReadAllLines(string.Format("inputs/Day{0:00}.txt", currentDayNumber));
string resultPart1 = currentDay.ResolvePart1(linesDay);
Console.WriteLine("Day{1:00} Result Part1: {0}", resultPart1, currentDayNumber);
string resultPart2 = currentDay.ResolvePart2(linesDay);
Console.WriteLine("Day{1:00} Result Part2: {0}", resultPart2, currentDayNumber);
Console.Read(); Console.Read();
} }

File diff suppressed because one or more lines are too long