feat(2025-04): Add day 4

This commit is contained in:
Josh Creek
2025-12-04 12:49:03 +00:00
parent 4eaa778adb
commit af8d99e953
8 changed files with 594 additions and 2 deletions
+134
View File
@@ -0,0 +1,134 @@
using AOC.Helpers;
namespace AOC.Tests.Y2025;
[TestFixture]
[Parallelizable(ParallelScope.All)]
public class Day04
{
[SetUp]
public void Setup()
{
realData = File.ReadAllLines(Path.Combine(TestContext.CurrentContext.TestDirectory, "Y2025", "Data",
$"{GetThisClassName()}.dat"));
}
protected string GetThisClassName() { return GetType().Name; }
private string[] realData;
[TestCase(@"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.", 13)]
[TestCase(null, 1451)] // The actual answer
public void Part1(string? input, int? expected)
{
// string[] lines = input != null ? new[] { input } : realData;
string[] lines = input != null ? input.Split("\n") : realData;
char[,] array = TwoDimensionalArrays.Make2DArrayFromStringArray(lines);
int rollsOfPaperThatCanBeAccessed = 0;
foreach ((int row, int column, char value) in TwoDimensionalArrays.Cells(array))
{
if (value != '@')
{
continue;
}
List<(int, int)> positionsWherePaperExists =
TwoDimensionalArrays.CheckAllSurroundingCellsForCharacter(array, row, column, '@');
if (positionsWherePaperExists.Count < 4)
{
rollsOfPaperThatCanBeAccessed++;
}
}
int result = rollsOfPaperThatCanBeAccessed;
if (expected != null)
{
Assert.That(result, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 1: {result}");
}
[TestCase(@"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.", 43)]
[TestCase(null, 8701)] // The actual answer
public void Part2(string? input, int? expected)
{
//string[] lines = input != null ? new[] { input } : realData;
string[] lines = input != null ? input.Split("\n") : realData;
char[,] array = TwoDimensionalArrays.Make2DArrayFromStringArray(lines);
int totalRollsOfPaperRemoved = 0;
int rollsOfPaperAccessed = 0;
bool continueLoop = true;
while (continueLoop)
{
List<(int, int)> positionsToRemovePaper = new();
foreach ((int row, int column, char value) in TwoDimensionalArrays.Cells(array))
{
if (value != '@')
{
continue;
}
List<(int, int)> positionsWherePaperExists =
TwoDimensionalArrays.CheckAllSurroundingCellsForCharacter(array, row, column, '@');
if (positionsWherePaperExists.Count < 4)
{
totalRollsOfPaperRemoved++;
rollsOfPaperAccessed++;
positionsToRemovePaper.Add((row, column));
}
}
if (rollsOfPaperAccessed == 0)
{
continueLoop = false;
}
rollsOfPaperAccessed = 0;
// Remove each roll
foreach ((int, int) position in positionsToRemovePaper)
{
array[position.Item1, position.Item2] = 'x';
}
positionsToRemovePaper.Clear();
}
int result = totalRollsOfPaperRemoved;
if (expected != null)
{
Assert.That(result, Is.EqualTo(expected.Value));
}
Console.WriteLine($"Part 2: {result}");
}
}