Completed day 3 and add it to test project

master
Alexander Sigler 3 years ago
parent e295f7a119
commit 494ad5f1dd

@ -30,6 +30,12 @@
<None Update="Input\Day2_test.txt"> <None Update="Input\Day2_test.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="Input\Day3_input.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Input\Day3_test.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -22,16 +22,20 @@ namespace AOC2021.Test
Console.WriteLine($"Testing {request.Day} Part A, Test data"); Console.WriteLine($"Testing {request.Day} Part A, Test data");
if (!string.IsNullOrEmpty(request.Answer.Day_A_Test)) 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; 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"); Console.WriteLine($"Testing {request.Day} Part A, Input data");
if (!string.IsNullOrEmpty(request.Answer.Day_A_Input)) 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; 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"); Console.WriteLine($"Testing {request.Day} Part B, Test data");
if (!string.IsNullOrEmpty(request.Answer.Day_B_Test)) 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; 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"); Console.WriteLine($"Testing {request.Day} Part B, Input data");
if (!string.IsNullOrEmpty(request.Answer.Day_B_Input)) 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; 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; return response;
} }
@ -59,6 +63,7 @@ namespace AOC2021.Test
var constructedFileName = $"Input\\{day}_{type}.txt"; var constructedFileName = $"Input\\{day}_{type}.txt";
if (file.ToLower().Equals(constructedFileName.ToLower())) if (file.ToLower().Equals(constructedFileName.ToLower()))
{ {
Console.WriteLine("Loading " + file);
return File.ReadAllText(file); return File.ReadAllText(file);
} }
} }

@ -30,5 +30,13 @@ namespace AOC2021.Tests
var result = _tester.Test(request); var result = _tester.Test(request);
Assert.IsTrue(request.Answer.Equals(result.Answer)); 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));
}
} }
} }

File diff suppressed because it is too large Load Diff

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

@ -1,4 +1,8 @@
using AOC2021.Models; using AOC2021.Models;
using AOC2021.Models.Day3;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AOC2021.Days namespace AOC2021.Days
{ {
@ -6,12 +10,85 @@ namespace AOC2021.Days
{ {
protected override AOCResponse ExecutePartA() protected override AOCResponse ExecutePartA()
{ {
throw new System.NotImplementedException(); var numbers = new List<BinaryNumber>();
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() 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<BinaryNumber> FilterOutBinary(bool mostCommon, int selectedBit, List<BinaryNumber> nums)
{
var zeroNums = new List<BinaryNumber>();
var oneNums = new List<BinaryNumber>();
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);
} }
} }
} }

@ -51,7 +51,7 @@ namespace AOC2021.Models
protected string[] GetSplitInput() protected string[] GetSplitInput()
{ {
return this._request.Input.Trim().Split("\n"); return this._request.Input.Trim().Replace("\r", "").Split("\n");
} }
public void SetLogger(ILogger logger) public void SetLogger(ILogger logger)
{ {

@ -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);
}
}
}
Loading…
Cancel
Save