Cleaned up Day1Solution and added unit tests for Day1Solution.
This commit is contained in:
parent
2f34e986aa
commit
34cdb1b500
|
@ -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;
|
Console.WriteLine($"Part 1 Result: {CalculatePart1(input)}");
|
||||||
bool part2Flag = false;
|
Console.WriteLine($"Part 2 Result: {CalculatePart2(input)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CalculatePart1(string[] input)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
for (var i = 0; i < input.Length; i++)
|
for (var i = 0; i < input.Length; i++)
|
||||||
{
|
sum += int.Parse(input[i]);
|
||||||
part1Result += int.Parse(input[i]);
|
|
||||||
}
|
|
||||||
Console.WriteLine($"Part 1 Result: {part1Result}");
|
|
||||||
|
|
||||||
part1Result = 0;
|
return sum;
|
||||||
while (!part2Flag)
|
}
|
||||||
|
|
||||||
|
public int CalculatePart2(string[] input)
|
||||||
|
{
|
||||||
|
Dictionary<int, int> frequencyOccurrences = new Dictionary<int, int>();
|
||||||
|
int sum = 0, result = 0;
|
||||||
|
bool foundResult = false;
|
||||||
|
|
||||||
|
frequencyOccurrences.Add(0, 1);
|
||||||
|
while (!foundResult)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < input.Length; i++)
|
foreach (var s in input)
|
||||||
{
|
{
|
||||||
part1Result += int.Parse(input[i]);
|
sum += int.Parse(s);
|
||||||
|
|
||||||
if (!frequencyOccurrences.ContainsKey(part1Result))
|
if (!frequencyOccurrences.ContainsKey(sum))
|
||||||
frequencyOccurrences.Add(part1Result, 0);
|
frequencyOccurrences.Add(sum, 0);
|
||||||
|
|
||||||
frequencyOccurrences[part1Result]++;
|
if (++frequencyOccurrences[sum] > 1)
|
||||||
|
|
||||||
if (frequencyOccurrences[part1Result] > 1)
|
|
||||||
{
|
{
|
||||||
part2Flag = true;
|
foundResult = true;
|
||||||
part2Result = part1Result;
|
result = sum;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Part 2 Result: {part2Result}");
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
using System;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
|
|
||||||
namespace AoC2018Test
|
|
||||||
{
|
|
||||||
[TestClass]
|
|
||||||
public class UnitTest1
|
|
||||||
{
|
|
||||||
[TestMethod]
|
|
||||||
public void TestMethod1()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue