feat: solved day 2 part 1...part 2 is WIP
This commit is contained in:
@@ -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>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace RedNosedReports;
|
||||
|
||||
static class Direction
|
||||
{
|
||||
public const string Unknown = "Unknown";
|
||||
public const string Increasing = "Increasing";
|
||||
public const string Decreasing = "Decreasing";
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using RedNosedReports;
|
||||
|
||||
if (args.Length is 0)
|
||||
{
|
||||
Console.WriteLine("Please provide a path to the input file.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (File.Exists(args[0]) is false)
|
||||
{
|
||||
Console.WriteLine("The provided file does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
var isPart2 = args.Length is 2 && args[1] is "part2";
|
||||
var input = await File.ReadAllLinesAsync(args[0]);
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var reports = new PuzzleParser().Parse(input);
|
||||
|
||||
var numberOfSafeReports = isPart2
|
||||
? reports.Count(r => r.IsSafeWithProblemDampener())
|
||||
: reports.Count(r => r.IsSafe());
|
||||
|
||||
stopwatch.Stop();
|
||||
Console.WriteLine($"The total number of safe reports is {numberOfSafeReports}. ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace RedNosedReports;
|
||||
|
||||
class PuzzleParser
|
||||
{
|
||||
public List<Report> Parse(string[] lines)
|
||||
{
|
||||
var reports = new List<Report>();
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var numbers = line.Split(' ').Select(int.Parse).ToList();
|
||||
reports.Add(new Report(numbers));
|
||||
}
|
||||
|
||||
return reports;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<NoWarn>CA1822</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\INPUT.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,81 @@
|
||||
namespace RedNosedReports;
|
||||
|
||||
class Report(List<int> levels)
|
||||
{
|
||||
private readonly List<int> _levels = levels;
|
||||
|
||||
public bool IsSafe()
|
||||
{
|
||||
for (var i = 0; i < _levels.Count - 1; i++)
|
||||
{
|
||||
if (CheckIfSafe(_levels[i], _levels[i + 1]) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsSafeWithProblemDampener()
|
||||
{
|
||||
for (var currentIndex = 0; currentIndex < _levels.Count - 1; currentIndex++)
|
||||
{
|
||||
var nextIndex = currentIndex + 1;
|
||||
var current = _levels[currentIndex];
|
||||
var next = _levels[nextIndex];
|
||||
|
||||
if (CheckIfSafe(current, next) is false)
|
||||
{
|
||||
var levelsWithoutCurrent = _levels.ToList();
|
||||
levelsWithoutCurrent.RemoveAt(currentIndex);
|
||||
var isSafeWithoutCurrent = new Report(levelsWithoutCurrent).IsSafe();
|
||||
|
||||
if (isSafeWithoutCurrent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var levelsWithoutNext = _levels.ToList();
|
||||
levelsWithoutNext.RemoveAt(nextIndex);
|
||||
var isSafeWithoutNext = new Report(levelsWithoutNext).IsSafe();
|
||||
|
||||
if (isSafeWithoutNext)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool CheckIfSafe(int current, int next)
|
||||
{
|
||||
var direction = _levels[1] > _levels[0]
|
||||
? Direction.Increasing
|
||||
: Direction.Decreasing;
|
||||
|
||||
var isNextGreaterThanCurrent = next > current;
|
||||
var delta = Math.Abs(next - current);
|
||||
|
||||
if (delta > 3 || delta < 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (direction is Direction.Increasing && isNextGreaterThanCurrent is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (direction is Direction.Decreasing && isNextGreaterThanCurrent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user