Cleaned up Day1Solution and added unit tests for Day1Solution.

This commit is contained in:
Brandon Scott 2018-12-08 23:21:45 -06:00
parent 2f34e986aa
commit 34cdb1b500
4 changed files with 105 additions and 33 deletions

View File

@ -16,38 +16,47 @@ namespace AoC2018.Solutions
public override void Run() public override void Run()
{ {
string[] input = File.ReadAllLines(Path.Combine("Input", "Day1Input.txt")); string[] input = File.ReadAllLines(Path.Combine("Input", "Day1Input.txt"));
Dictionary<int, int> frequencyOccurrences = new Dictionary<int, int>();
int part1Result = 0, part2Result = 0;
bool part2Flag = false;
for (var i = 0; i < input.Length; i++) Console.WriteLine($"Part 1 Result: {CalculatePart1(input)}");
{ Console.WriteLine($"Part 2 Result: {CalculatePart2(input)}");
part1Result += int.Parse(input[i]);
} }
Console.WriteLine($"Part 1 Result: {part1Result}");
part1Result = 0; public int CalculatePart1(string[] input)
while (!part2Flag)
{ {
int sum = 0;
for (var i = 0; i < input.Length; i++) for (var i = 0; i < input.Length; i++)
sum += int.Parse(input[i]);
return sum;
}
public int CalculatePart2(string[] input)
{ {
part1Result += int.Parse(input[i]); Dictionary<int, int> frequencyOccurrences = new Dictionary<int, int>();
int sum = 0, result = 0;
bool foundResult = false;
if (!frequencyOccurrences.ContainsKey(part1Result)) frequencyOccurrences.Add(0, 1);
frequencyOccurrences.Add(part1Result, 0); while (!foundResult)
frequencyOccurrences[part1Result]++;
if (frequencyOccurrences[part1Result] > 1)
{ {
part2Flag = true; foreach (var s in input)
part2Result = part1Result; {
sum += int.Parse(s);
if (!frequencyOccurrences.ContainsKey(sum))
frequencyOccurrences.Add(sum, 0);
if (++frequencyOccurrences[sum] > 1)
{
foundResult = true;
result = sum;
break; break;
} }
} }
} }
Console.WriteLine($"Part 2 Result: {part2Result}"); return result;
} }
} }
} }

View File

@ -14,4 +14,8 @@
<ProjectReference Include="..\AoC2018\AoC2018.csproj" /> <ProjectReference Include="..\AoC2018\AoC2018.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AoC2018.Solutions;
namespace AoC2018Test.Solutions
{
[TestClass]
public class Day1SolutionTests
{
[TestMethod]
public void TestPart1()
{
Day1Solution target = new Day1Solution();
// Example 1
var result = target.CalculatePart1(new string[]
{
"+1", "+1", "+1"
});
Assert.AreEqual(3, result);
// Example 2
result = target.CalculatePart1(new string[]
{
"+1", "+1", "-2"
});
Assert.AreEqual(0, result);
// Example 3
result = target.CalculatePart1(new string[]
{
"-1", "-2", "-3"
});
Assert.AreEqual(-6, result);
}
[TestMethod]
public void TestPart2()
{
Day1Solution target = new Day1Solution();
// Example 1
var result = target.CalculatePart2(new string[]
{
"+1", "-1"
});
Assert.AreEqual(0, result);
// Example 2
result = target.CalculatePart2(new string[]
{
"+3", "+3", "+4", "-2", "-4"
});
Assert.AreEqual(10, result);
// Example 3
result = target.CalculatePart2(new string[]
{
"-6", "+3", "+8", "+5", "-6"
});
Assert.AreEqual(5, result);
// Example 4
result = target.CalculatePart2(new string[]
{
"+7", "+7", "-2", "-7", "-4"
});
Assert.AreEqual(14, result);
}
}
}

View File

@ -1,14 +0,0 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AoC2018Test
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}