From 34cdb1b5006d2969a1b6b434ee13bf2ea2ae1bbe Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Sat, 8 Dec 2018 23:21:45 -0600 Subject: [PATCH] Cleaned up Day1Solution and added unit tests for Day1Solution. --- AoC2018/Solutions/Day1Solution.cs | 47 ++++++++------ AoC2018Test/AoC2018Test.csproj | 4 ++ AoC2018Test/Solutions/Day1SolutionTests.cs | 73 ++++++++++++++++++++++ AoC2018Test/UnitTest1.cs | 14 ----- 4 files changed, 105 insertions(+), 33 deletions(-) create mode 100644 AoC2018Test/Solutions/Day1SolutionTests.cs delete mode 100644 AoC2018Test/UnitTest1.cs diff --git a/AoC2018/Solutions/Day1Solution.cs b/AoC2018/Solutions/Day1Solution.cs index 8456e15..a253cd5 100644 --- a/AoC2018/Solutions/Day1Solution.cs +++ b/AoC2018/Solutions/Day1Solution.cs @@ -16,38 +16,47 @@ namespace AoC2018.Solutions public override void Run() { string[] input = File.ReadAllLines(Path.Combine("Input", "Day1Input.txt")); - Dictionary frequencyOccurrences = new Dictionary(); - int part1Result = 0, part2Result = 0; - bool part2Flag = false; + + Console.WriteLine($"Part 1 Result: {CalculatePart1(input)}"); + Console.WriteLine($"Part 2 Result: {CalculatePart2(input)}"); + } + + public int CalculatePart1(string[] input) + { + int sum = 0; for (var i = 0; i < input.Length; i++) - { - part1Result += int.Parse(input[i]); - } - Console.WriteLine($"Part 1 Result: {part1Result}"); + sum += int.Parse(input[i]); - part1Result = 0; - while (!part2Flag) + return sum; + } + + public int CalculatePart2(string[] input) + { + Dictionary frequencyOccurrences = new Dictionary(); + 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)) - frequencyOccurrences.Add(part1Result, 0); + if (!frequencyOccurrences.ContainsKey(sum)) + frequencyOccurrences.Add(sum, 0); - frequencyOccurrences[part1Result]++; - - if (frequencyOccurrences[part1Result] > 1) + if (++frequencyOccurrences[sum] > 1) { - part2Flag = true; - part2Result = part1Result; + foundResult = true; + result = sum; break; } } } - Console.WriteLine($"Part 2 Result: {part2Result}"); + return result; } } } diff --git a/AoC2018Test/AoC2018Test.csproj b/AoC2018Test/AoC2018Test.csproj index 5553ce3..f3d6f50 100644 --- a/AoC2018Test/AoC2018Test.csproj +++ b/AoC2018Test/AoC2018Test.csproj @@ -14,4 +14,8 @@ + + + + diff --git a/AoC2018Test/Solutions/Day1SolutionTests.cs b/AoC2018Test/Solutions/Day1SolutionTests.cs new file mode 100644 index 0000000..ac7e1d5 --- /dev/null +++ b/AoC2018Test/Solutions/Day1SolutionTests.cs @@ -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); + } + } +} diff --git a/AoC2018Test/UnitTest1.cs b/AoC2018Test/UnitTest1.cs deleted file mode 100644 index 87347ec..0000000 --- a/AoC2018Test/UnitTest1.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace AoC2018Test -{ - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestMethod1() - { - } - } -}