feat: solved day 2 part 1...part 2 is WIP

This commit is contained in:
Stevan Freeborn
2024-12-03 02:32:30 -06:00
parent 17f6c01170
commit 60b5c726cf
10 changed files with 340 additions and 1 deletions
@@ -0,0 +1,31 @@
namespace RedNosedReports.Tests;
public class PuzzleParserTests
{
private readonly PuzzleParser _puzzleParser = new();
[Test]
public async Task Parse_WhenGivenInput_ItShouldReturnListOfReports()
{
string[] input = [
"7 6 4 2 1",
"1 2 7 8 9",
"9 7 6 2 1",
"1 3 2 4 5",
"8 6 4 4 1",
"1 3 6 7 9",
];
var result = _puzzleParser.Parse(input);
await Assert.That(result).IsEquivalentTo(new List<Report>()
{
new([7, 6, 4, 2, 1]),
new([1, 2, 7, 8, 9]),
new([9, 7, 6, 2, 1]),
new([1, 3, 2, 4, 5]),
new([8, 6, 4, 4, 1]),
new([1, 3, 6, 7, 9]),
});
}
}
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>CA1822</NoWarn>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\RedNosedReports\RedNosedReports.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TUnit" Version="0.4.45" />
</ItemGroup>
</Project>
+102
View File
@@ -0,0 +1,102 @@
namespace RedNosedReports.Tests;
public class ReportTests
{
private readonly PuzzleParser _puzzleParser = new();
private async Task<List<Report>> GetPuzzleLists()
{
var inputPath = Path.Combine(AppContext.BaseDirectory, "INPUT.txt");
var input = await File.ReadAllLinesAsync(inputPath);
return _puzzleParser.Parse(input);
}
[Test]
public async Task IsSafe_WhenGivenPuzzleInput_ItShouldReturnExpectedCount()
{
var reports = await GetPuzzleLists();
var count = reports.Count(r => r.IsSafe());
await Assert.That(count).IsEqualTo(407);
}
[Test]
public async Task IsSafe_WhenAllLevelsAreNotDecreasing_ItShouldReturnFalse()
{
var report = new Report([7, 6, 9, 2, 1]);
await Assert.That(report.IsSafe()).IsEqualTo(false);
}
[Test]
public async Task IsSafe_WhenAllLevelsAreDecreasing_ItShouldReturnTrue()
{
var report = new Report([7, 6, 4, 2, 1]);
await Assert.That(report.IsSafe()).IsEqualTo(true);
}
[Test]
public async Task IsSafe_WhenAllLevelsAreNotIncreasing_ItShouldReturnFalse()
{
var report = new Report([1, 3, 6, 4, 9]);
await Assert.That(report.IsSafe()).IsEqualTo(false);
}
[Test]
public async Task IsSafe_WhenAllLevelsAreIncreasing_ItShouldReturnTrue()
{
var report = new Report([1, 3, 6, 7, 9]);
await Assert.That(report.IsSafe()).IsEqualTo(true);
}
[Test]
public async Task IsSafe_WhenTheChangeIsTooLarge_ItShouldReturnFalse()
{
var report = new Report([1, 2, 7, 8, 9]);
await Assert.That(report.IsSafe()).IsEqualTo(false);
}
[Test]
public async Task IsSafe_WhenTheChangeIsTooSmall_ItShouldReturnFalse()
{
var report = new Report([8, 6, 4, 4, 1]);
await Assert.That(report.IsSafe()).IsEqualTo(false);
}
[Test]
public async Task IsSafeWithProblemDampener_WhenSafeByRemovingOneLevel_ItShouldReturnTrue()
{
var reportOne = new Report([1, 3, 2, 4, 5]);
var reportTwo = new Report([8, 6, 4, 4, 1]);
var reportThree = new Report([19, 21, 24, 27, 24]);
await Assert.That(reportOne.IsSafeWithProblemDampener()).IsEqualTo(true);
await Assert.That(reportTwo.IsSafeWithProblemDampener()).IsEqualTo(true);
await Assert.That(reportThree.IsSafeWithProblemDampener()).IsEqualTo(true);
}
[Test]
public async Task IsSafeWithProblemDampener_WhenSafeByRemovingNoLevels_ItShouldReturnTrue()
{
var reportOne = new Report([7, 6, 4, 2, 1]);
var reportTwo = new Report([1, 3, 6, 7, 9]);
await Assert.That(reportOne.IsSafeWithProblemDampener()).IsEqualTo(true);
await Assert.That(reportTwo.IsSafeWithProblemDampener()).IsEqualTo(true);
}
[Test]
public async Task IsSafeWithProblemDampener_WhenUnsafeRegardlessOfLevelsRemoved_ItShouldReturnFalse()
{
var reportOne = new Report([1, 2, 7, 8, 9]);
var reportTwo = new Report([9, 7, 6, 2, 1]);
await Assert.That(reportOne.IsSafeWithProblemDampener()).IsEqualTo(false);
await Assert.That(reportTwo.IsSafeWithProblemDampener()).IsEqualTo(false);
}
}