diff --git a/AOC2021.Test/AOC2021.Test.csproj b/AOC2021.Test/AOC2021.Test.csproj index 85491be..077343f 100644 --- a/AOC2021.Test/AOC2021.Test.csproj +++ b/AOC2021.Test/AOC2021.Test.csproj @@ -30,6 +30,12 @@ Always + + Always + + + Always + diff --git a/AOC2021.Test/AOCTester.cs b/AOC2021.Test/AOCTester.cs index e6d9e5f..9da224c 100644 --- a/AOC2021.Test/AOCTester.cs +++ b/AOC2021.Test/AOCTester.cs @@ -22,16 +22,20 @@ namespace AOC2021.Test Console.WriteLine($"Testing {request.Day} Part A, Test data"); if (!string.IsNullOrEmpty(request.Answer.Day_A_Test)) response.Answer.Day_A_Test = GetAOCDay(request.Day).ExecuteDay(new AOCRequest() { Input = test, Version = AOCVersion.A }).Answer; + Console.WriteLine(response.Answer.Day_A_Test); Console.WriteLine($"Testing {request.Day} Part A, Input data"); if (!string.IsNullOrEmpty(request.Answer.Day_A_Input)) response.Answer.Day_A_Input = GetAOCDay(request.Day).ExecuteDay(new AOCRequest() { Input = input, Version = AOCVersion.A }).Answer; + Console.WriteLine(response.Answer.Day_A_Input); Console.WriteLine($"Testing {request.Day} Part B, Test data"); if (!string.IsNullOrEmpty(request.Answer.Day_B_Test)) response.Answer.Day_B_Test = GetAOCDay(request.Day).ExecuteDay(new AOCRequest() { Input = test, Version = AOCVersion.B }).Answer; + Console.WriteLine(response.Answer.Day_B_Test); Console.WriteLine($"Testing {request.Day} Part B, Input data"); if (!string.IsNullOrEmpty(request.Answer.Day_B_Input)) response.Answer.Day_B_Input = GetAOCDay(request.Day).ExecuteDay(new AOCRequest() { Input = input, Version = AOCVersion.B }).Answer; + Console.WriteLine(response.Answer.Day_B_Input); return response; } @@ -59,6 +63,7 @@ namespace AOC2021.Test var constructedFileName = $"Input\\{day}_{type}.txt"; if (file.ToLower().Equals(constructedFileName.ToLower())) { + Console.WriteLine("Loading " + file); return File.ReadAllText(file); } } diff --git a/AOC2021.Test/DayTest.cs b/AOC2021.Test/DayTest.cs index fc84c2a..8b1aef5 100644 --- a/AOC2021.Test/DayTest.cs +++ b/AOC2021.Test/DayTest.cs @@ -30,5 +30,13 @@ namespace AOC2021.Tests var result = _tester.Test(request); Assert.IsTrue(request.Answer.Equals(result.Answer)); } + + [TestMethod] + public void Day3() + { + var request = new TestRequest() { Day = "day3", Answer = new Answer() { Day_A_Test = "198", Day_A_Input = "3148794", Day_B_Test = "230", Day_B_Input = "2795310" } }; + var result = _tester.Test(request); + Assert.IsTrue(request.Answer.Equals(result.Answer)); + } } } diff --git a/AOC2021.Test/Input/Day3_input.txt b/AOC2021.Test/Input/Day3_input.txt new file mode 100644 index 0000000..3a9a2f1 --- /dev/null +++ b/AOC2021.Test/Input/Day3_input.txt @@ -0,0 +1,1000 @@ +101010000100 +100001010100 +111100000101 +010000000010 +001101100010 +100110110101 +110100101101 +101010000001 +110011110000 +110000011011 +010111010100 +000101101001 +110001101100 +110100111101 +000101101100 +011111101000 +001011101100 +001011000110 +000011110110 +101110010111 +010011101001 +011111100101 +000001110101 +100001010010 +001110010101 +111010000110 +100111001111 +110010000101 +001111100001 +000110010101 +111001111011 +110000111111 +011101110001 +111001000011 +100110101011 +101101100010 +110100100101 +101011111000 +001101110011 +011000010011 +101010011111 +101011000110 +011111101001 +000011100000 +101111000101 +010000010111 +010110111000 +101110111100 +000001010011 +011011000001 +110101101011 +011110101100 +010001011100 +110111000011 +010101101001 +110011011111 +111101111101 +110011010111 +100100100110 +011100101111 +100111010000 +110000110111 +111000010111 +100001010001 +101010001111 +010101100111 +001010101111 +000001011011 +111100011110 +101110100010 +011111011110 +111110111011 +001110110111 +010011010000 +111011011101 +101110110100 +111010011111 +011011011101 +110001110100 +000010011010 +011101010101 +100000001000 +000100100001 +011110000000 +111100101010 +001100100010 +101001101110 +111010001110 +101011111101 +010011100101 +110111110101 +111000111010 +010000000111 +001101111111 +101101001100 +111001000100 +001100000000 +011011100100 +010011110000 +000001111110 +110011001010 +001001000110 +011011001101 +001111111111 +001010101010 +011110110100 +110101111111 +001111011101 +001101001111 +000000111100 +001101101010 +011000001001 +100100101101 +000000111011 +011101110011 +101011110010 +010110000010 +000101000000 +010100111011 +000111010011 +111011111000 +011110010000 +000011101001 +101001110000 +011101111101 +100010010100 +011100001011 +110011011000 +100010001110 +100101010111 +100101101110 +010010000011 +101011000111 +100100100111 +001000001000 +110010100011 +111100001011 +011010001000 +000011010000 +000111111011 +110100101110 +011110000011 +100101000011 +001100100001 +000010100011 +111101111011 +001010101101 +101110110010 +101011011010 +111010101011 +001110010001 +111110101011 +111111001011 +011100011101 +111100111001 +101011000000 +100000010111 +000101111101 +001011001110 +111101101010 +110001000000 +101111110100 +010111010110 +111011100001 +111100010100 +000100011100 +000011011011 +010000001001 +100100011001 +001010011110 +110001110011 +100010000110 +001110001101 +111001111111 +100100000011 +001001101011 +100101010011 +010111100010 +001110101100 +000110111111 +010011101101 +011101011011 +101000111100 +101100001011 +000011100011 +111110110011 +100011001101 +000100100011 +110011111010 +011001100111 +000011100110 +011001001110 +000111011110 +010101111101 +101100101011 +111110101111 +101101011010 +000101110111 +101011010010 +101100101010 +111101100100 +011100101000 +100111100010 +000110110011 +000000100001 +011110001110 +000111110010 +001100011001 +000001111001 +001111011111 +110011001111 +101010110101 +011010110100 +100010110011 +111111111101 +010001110011 +001101110000 +001110100011 +110010101011 +001010001000 +101010010000 +000101011111 +111000111101 +011101010110 +100111010011 +010010010000 +111000101101 +110000110010 +010110010001 +011101001110 +001010011111 +110011010001 +100000100001 +110110001100 +111110101101 +101110011011 +100101011011 +001010011010 +111101000110 +100010011000 +001101101001 +010001010100 +011100110010 +011010100110 +101111011110 +100011010111 +000011111110 +000110101011 +000011111000 +101110101100 +101100011110 +011010101011 +010001110111 +101010100001 +011011011001 +110011101011 +110001011111 +000010101100 +100000011101 +010000101000 +001111101100 +111100011100 +101110101101 +110111110100 +010101000001 +011110000001 +111100010101 +011110010001 +001101001100 +001011011100 +010110101111 +000100101011 +001100001010 +000101101111 +010111110110 +100010111011 +000110001011 +010111010011 +101001000101 +011000111101 +001001101001 +010100010000 +111111001000 +011110100011 +011000101100 +111101110100 +101010010110 +111101010111 +011001100100 +001110001110 +111110110000 +110100011111 +000101101110 +011000110011 +111001111110 +011110101110 +010011001101 +000011011111 +111001010111 +000000111101 +100101100100 +100001110101 +111100100101 +001110000110 +011100001101 +100100110011 +001100000100 +010011001110 +111111110011 +101110111111 +100111011000 +111110101110 +100010101100 +010101010000 +011101100001 +010010011101 +001111010111 +011100010101 +111101110101 +010100001110 +011000111000 +100110100101 +110101001111 +000011011010 +110101011010 +110010111100 +110111000101 +100111000100 +110101011111 +110110011101 +010111101010 +111001010011 +001001100100 +001101101101 +011100001100 +000110010011 +010101010110 +000011001101 +101001001100 +111011100011 +011011110100 +001110100001 +001000101001 +001100010111 +110000001101 +100010111101 +000110111101 +111001101101 +101110100000 +101001100101 +101100011001 +111010101101 +010101011100 +111100011000 +101111110101 +110111110011 +000000101100 +010110101011 +100111011011 +010111011000 +000101110100 +001010110101 +000010111011 +001110000001 +010000010000 +101110010101 +000000111110 +110100011000 +110110111100 +100010110100 +001000101110 +011001010101 +000100010001 +100011100010 +100011100000 +100010101001 +100101111000 +110000011110 +011000011101 +101101101101 +100110010110 +111110001000 +111001011010 +001110110001 +100000010101 +111101001111 +000010010100 +101100110001 +100101011000 +010101001010 +000111100000 +100010101000 +011101110100 +110110001110 +010001000110 +100101001100 +110000010111 +011000101111 +001111001000 +001110010010 +000010101101 +011000010000 +011011101010 +001010000101 +001010110010 +100001100000 +110111010000 +100010001101 +010011001011 +110011001110 +110111000110 +000001110010 +110010001001 +111100100100 +000101110110 +000100011001 +111111010101 +011010100000 +110111000100 +011101011010 +100011111110 +100001001101 +100111000001 +100001011111 +010010000010 +010111101011 +000001111100 +001100110111 +100111011100 +101000101100 +100111111111 +101101000110 +101111001111 +011010110101 +111000110000 +100010101101 +011110100100 +001101110101 +011101011000 +010101011000 +010110110011 +110101011110 +111010101100 +110111000000 +101011110110 +100111101010 +110001111110 +001000111001 +110001111011 +011010000100 +000101001010 +010111000001 +100000101000 +011010111001 +010011101110 +011011010101 +010110101100 +000010100110 +011111101011 +101111100110 +110010001100 +100000110100 +100111100011 +110111101010 +110010010000 +000011010010 +111000000111 +100101100101 +100011101110 +111110110110 +111001011000 +010100111101 +111110001100 +000110011101 +000010110010 +101000111011 +101000010100 +001001110001 +111010101111 +100011000101 +111011001100 +000000011100 +101010111001 +110111101000 +011000110001 +100100110010 +011101011101 +001011111111 +101011100110 +101011010111 +100001011010 +100100010100 +000010000010 +101101010111 +001011001101 +101100111010 +110101000001 +101101110100 +100100110111 +110001101001 +010101010001 +110110001111 +000011011101 +111111110001 +011110110101 +111100110101 +011111011011 +101001111110 +110101101010 +111001000001 +010100110001 +111011100101 +101011101101 +010010010001 +111110001001 +100100111000 +111110111100 +001000000000 +111110111101 +110010101101 +100110111110 +001010100010 +101011001001 +111101010100 +110011100000 +100111000111 +000011000110 +101010100000 +101011011000 +000010110110 +111100000011 +000000010010 +101010110111 +001111111000 +100011110101 +001001000010 +111011101110 +001111011100 +111011101010 +011011000101 +000001000010 +001000100100 +001110110011 +010101011011 +100011011010 +111000001110 +110010000000 +100101100010 +100111110011 +010011100001 +010001111010 +110101010101 +101111000001 +001001011100 +011110000110 +111001011110 +111001111010 +110100010011 +001000010010 +111110000010 +001000011001 +010100011101 +101111101011 +010111001111 +111011011010 +111101111010 +001111001111 +111110100000 +100000011000 +110100111100 +001011101000 +010011011001 +010000010011 +100010011010 +110001111111 +011010111011 +101001111100 +111110111000 +110000001100 +010000100101 +110101100111 +110011100010 +110001001101 +011101111011 +011010101101 +011000011111 +101011101001 +010100000010 +110100100010 +101010101101 +010000111101 +110100110110 +101100001100 +001011100011 +011110011001 +000000001010 +000010111001 +001000001010 +100101001010 +010000000011 +101011101100 +011101010111 +100100101001 +110100100100 +110101001101 +010100010111 +011001001100 +101100111001 +100001001011 +101100011111 +001111101101 +010011110101 +011100010011 +000010011101 +011110001101 +010001010011 +001001111001 +100111110100 +111011000111 +101010110000 +010010111011 +100010110111 +110011111110 +101111000111 +111011000000 +110001110001 +001100100011 +001011100111 +101001110010 +111001001101 +000010000011 +001011010010 +110111101110 +001000010110 +000111000111 +010001101010 +011001110101 +011000110000 +000111100100 +101010111101 +010010010010 +101111001001 +001100100111 +100111111000 +100000010011 +101101111001 +000110011110 +100001111101 +110000100001 +101110110110 +111010000010 +101010001010 +000001010000 +101011110100 +001101010110 +100011010011 +101011101110 +011101101101 +100101010110 +101011100111 +100010010000 +000101011101 +011000001000 +111011111110 +101001101111 +101000111001 +100010100111 +011111000101 +100010001111 +110011011101 +111101100111 +111101100000 +010111000100 +111010010010 +001011100100 +001000110100 +100110010111 +111010011100 +100101001101 +101001110110 +011001110010 +000111010110 +010000011101 +101100100001 +101001001000 +000111100011 +100110110000 +110011001001 +001011001001 +110101011100 +010001100101 +000000101010 +001001100001 +101001100011 +110001110101 +111001010010 +111011011000 +101101011000 +101001000010 +010110101110 +001000101101 +111001100111 +011110110000 +100100101100 +010100011111 +111000000000 +001000010111 +000000111010 +111011111001 +000110100011 +011110100110 +011011101100 +000000001101 +100101111011 +101110111001 +100111001110 +101110000101 +001110001111 +001000110010 +111101000011 +000100011011 +010111010101 +001100100000 +100101101011 +010111000011 +010101111010 +011000000111 +011011000110 +101001011100 +101101100000 +101110101001 +111000111110 +110010000010 +110111111001 +100100111011 +111011010011 +100001101000 +111110001110 +110100011010 +010101000111 +000110100111 +001111000101 +000111000011 +110001001001 +011000100010 +101001111011 +110001000101 +110111010011 +011100011011 +011011001011 +100101011111 +000000101000 +100001100110 +000100110101 +110100100000 +000101000001 +101111001100 +111101011100 +010001110110 +000001001100 +100100001110 +000100100111 +001101000001 +100011100110 +110110110010 +110001011001 +110001001000 +001011100010 +111011001011 +100101111110 +001001111011 +110101111010 +101100111100 +101111101000 +111110100101 +011110111101 +010110111101 +111010100010 +111100001100 +000001000111 +111011000100 +101000111000 +011101100100 +011000100000 +110001111101 +111010100011 +110010001010 +000101001111 +010110001001 +101101101010 +101100110010 +010010000111 +111110100010 +011011000011 +111111011011 +001011001000 +010001011101 +101101110010 +010001001101 +101000110101 +100100010011 +101010010001 +101010110011 +101110011110 +001010000110 +000011000000 +101111011000 +100001010110 +010111101101 +001110011011 +100100011110 +100001011001 +010100111111 +011101000110 +111010111011 +000010100100 +010011100111 +110001000011 +000100111010 +100111001000 +010110011000 +101101110101 +110101001011 +000101101010 +011110101111 +110111100111 +111100111011 +001000101011 +000010000101 +110101101101 +010000100111 +100110001011 +011001000110 +111101001000 +000010011000 +110101100001 +100100101111 +110111001010 +111011111011 +010111001000 +011011110011 +111011110010 +100011000111 +011001100011 +001111000000 +101001101010 +111111011101 +000100110001 +111010010000 +011111100000 +101110100101 +100011011110 +001000010001 +110110100000 +100011111011 +100011000001 +000011010100 +110101110110 +001100010010 +111011011001 +010101101111 +110111100100 +101000110100 +110001010100 +100010010111 +000111110111 +101101101100 +110100110001 +011011100000 +010011000000 +110101101000 +100011110001 +001011111101 +011000110110 +011101101010 +000011111100 +010110110111 +111101111001 +110111011101 +010001011000 +100110000101 +011101100101 +111101011000 +011110001000 +001011001100 +110110011111 +010111011100 +001111001110 +110010110110 +111011110110 +100000101100 +111100101011 +000100011110 +010111110000 +110011010101 +110000111110 +011100100011 +000101011000 +101110011100 +010111111000 +110010111101 +000001011010 +100010101011 +110001011100 +100100001100 +010101101010 +001101011110 +011011101111 +011100110011 +100011110000 +011110011111 +001011011101 +011010110010 +101000110111 +001001100111 +010111101111 +110110010001 +110001100000 +011001100101 +000110110111 +010010001100 +101100001010 +101010011110 +001010010110 +101001001001 +100110000010 +011101101111 +011010011001 +010111000010 +101001111111 +000010001001 +101000100011 +001110100100 +011011010000 +100111110101 +100110100000 +000101011001 +001100010110 +110011110010 +011000100001 +001100010101 +110010001101 +100011101001 +001100011110 +110101111101 +111111101000 +000011110001 +010000010100 +001101011001 +100111000000 +011100000101 +110110000000 +110011011001 +001100010011 +110011110110 +010010011111 +100001101001 +011100111100 +001011110001 +001010010011 +010000000101 +000100010011 +111101011001 +111100110110 +111000011100 +111101101110 +000101111011 +010101110101 +011110111111 +100000000011 +111000000011 +000010100000 +001110100010 +011001000010 +100001011110 +010100100111 +000111110011 +110100110101 +011001111100 +111101110110 +001101010101 +110110000010 +010101001101 +110100110000 +000100010101 +111011101001 +110111111111 +000000110010 +100111110110 +001000011111 +100100000101 +011001110110 +010010100110 +000101010111 +101001100000 +110011101110 +010001100001 +011100000100 +010100100000 +100000100100 \ No newline at end of file diff --git a/AOC2021.Test/Input/Day3_test.txt b/AOC2021.Test/Input/Day3_test.txt new file mode 100644 index 0000000..5ca60e4 --- /dev/null +++ b/AOC2021.Test/Input/Day3_test.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 \ No newline at end of file diff --git a/AOC2021/Days/Day3.cs b/AOC2021/Days/Day3.cs index ac847d7..af1c60b 100644 --- a/AOC2021/Days/Day3.cs +++ b/AOC2021/Days/Day3.cs @@ -1,4 +1,8 @@ using AOC2021.Models; +using AOC2021.Models.Day3; +using System; +using System.Collections.Generic; +using System.Linq; namespace AOC2021.Days { @@ -6,12 +10,85 @@ namespace AOC2021.Days { protected override AOCResponse ExecutePartA() { - throw new System.NotImplementedException(); + var numbers = new List(); + foreach (var bNum in GetSplitInput()) + { + numbers.Add(new BinaryNumber(bNum)); + } + + var length = numbers.FirstOrDefault().Size(); + var gamma = string.Empty; + for (int i = 0; i < length; i++) + { + int zero = 0; + int one = 0; + foreach (var bNum in numbers) + { + if (bNum.GetIntAt(i) == 0) + zero++; + else + one++; + } + Log($"Bit {i} is {zero} or {one}"); + gamma += (zero > one ? "0" : "1").ToString(); + } + var epsilon = string.Concat(gamma.Select(x => x == '0' ? '1' : '0')); //Not of gamma + Log($"Gamma rate is {gamma} or {ToInt(gamma)}"); + Log($"Episode rate is {epsilon} or {ToInt(epsilon)}"); + this._response.Answer = (ToInt(gamma) * ToInt(epsilon)).ToString(); + Log($"Final answer is {this._response.Answer}"); + + return this._response; } protected override AOCResponse ExecutePartB() { - throw new System.NotImplementedException(); + var initialNumbers = GetSplitInput().Select(x => new BinaryNumber(x)).ToList(); + Log($"An initial count of numbers is {initialNumbers.Count}"); + + int count = 0; + var oxygenRate = FilterOutBinary(true, count, initialNumbers); + while (oxygenRate.Count > 1) + { + oxygenRate = FilterOutBinary(true, ++count, oxygenRate); + Log($"Oxygen rate has {oxygenRate.Count} numbers left"); + } + + + count = 0; + var co2Rate = FilterOutBinary(false, count, initialNumbers); + while (co2Rate.Count > 1) + { + co2Rate = FilterOutBinary(false, ++count, co2Rate); + Log($"CO2 rate has {co2Rate.Count} numbers left"); + } + + Log("Oxygen rate is " + oxygenRate.FirstOrDefault().ToInt()); + Log("CO2 rate is " + co2Rate.FirstOrDefault().ToInt()); + this._response.Answer = (oxygenRate.FirstOrDefault().ToInt() * co2Rate.FirstOrDefault().ToInt()).ToString(); + return this._response; + } + + public List FilterOutBinary(bool mostCommon, int selectedBit, List nums) + { + var zeroNums = new List(); + var oneNums = new List(); + foreach (var num in nums) + { + if (num.GetIntAt(selectedBit) == 0) + zeroNums.Add(num); + else + oneNums.Add(num); + } + //If the zero count is greater return zeroNums, if equal or one count is higher return oneNums + if (mostCommon) + return zeroNums.Count > oneNums.Count ? zeroNums : oneNums; + else + return zeroNums.Count <= oneNums.Count ? zeroNums : oneNums; + } + public int ToInt(string s) + { + return Convert.ToInt32(s, 2); } } } diff --git a/AOC2021/Models/AOCDay.cs b/AOC2021/Models/AOCDay.cs index 12dbca3..debd0fc 100644 --- a/AOC2021/Models/AOCDay.cs +++ b/AOC2021/Models/AOCDay.cs @@ -51,7 +51,7 @@ namespace AOC2021.Models protected string[] GetSplitInput() { - return this._request.Input.Trim().Split("\n"); + return this._request.Input.Trim().Replace("\r", "").Split("\n"); } public void SetLogger(ILogger logger) { diff --git a/AOC2021/Models/Day3/BinaryNumber.cs b/AOC2021/Models/Day3/BinaryNumber.cs new file mode 100644 index 0000000..d78896b --- /dev/null +++ b/AOC2021/Models/Day3/BinaryNumber.cs @@ -0,0 +1,28 @@ +using System; + +namespace AOC2021.Models.Day3 +{ + public class BinaryNumber + { + private string _val; + public BinaryNumber(string s) + { + _val = s; + } + + public int GetIntAt(int pos) + { + return Convert.ToInt32(_val[pos].ToString()); + } + + public int Size() + { + return _val.Length; + } + + public int ToInt() + { + return Convert.ToInt32(_val, 2); + } + } +}