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
+8
View File
@@ -0,0 +1,8 @@
namespace RedNosedReports;
static class Direction
{
public const string Unknown = "Unknown";
public const string Increasing = "Increasing";
public const string Decreasing = "Decreasing";
}
+30
View File
@@ -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)");
+17
View File
@@ -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;
}
}
+21
View File
@@ -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>
+81
View File
@@ -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;
}
}