Add 2026 Day04 solution and tests
This commit is contained in:
1 parent
20c08e9ce6
commit
e643951d0e
2 files changed
+118
No files matched your search
@@ -0,0 +1,47 @@
|
|||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
|
||||||
|
namespace AoC_2026.Tests;
|
||||||
|
|
||||||
|
public class Day04Test
|
||||||
|
{
|
||||||
|
private readonly IPuzzleSolver<long> _sut;
|
||||||
|
private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||||||
|
private readonly string TestInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Test/day04.txt";
|
||||||
|
private readonly string ProdInputPath = @$"{rootPath}/AoC-PuzzleInputs/2026/Prod/day04.txt";
|
||||||
|
|
||||||
|
public Day04Test()
|
||||||
|
{
|
||||||
|
_sut = new Day04();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Part01_Test_equals_16()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart1(TestInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(16, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Part01_Prod_equals_7897()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart1(ProdInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(7897, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Part02_Test_equals_29()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart2(TestInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(29, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Part02_Prod_equals_1589()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart2(ProdInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(1589, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
|
||||||
|
namespace AoC_2026;
|
||||||
|
|
||||||
|
public class Day04 : IPuzzleSolver<long>
|
||||||
|
{
|
||||||
|
private static string[] ParsePuzzleInput(string path)
|
||||||
|
{
|
||||||
|
var puzzleInput = File.ReadAllLines(path)
|
||||||
|
.Where(l => !string.IsNullOrWhiteSpace(l))
|
||||||
|
.ToArray();
|
||||||
|
return puzzleInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long SolvePart1(string pathToPuzzleInput)
|
||||||
|
{
|
||||||
|
var barcodes = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
|
var sum = barcodes.Sum(bc => GetMinInstructionLength(bc));
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long SolvePart2(string pathToPuzzleInput)
|
||||||
|
{
|
||||||
|
var barcodes = File.ReadAllLines(pathToPuzzleInput);
|
||||||
|
var barcode = barcodes.Aggregate("", (acc, next) => acc + next);
|
||||||
|
var patternLength = GetMinInstructionLength(barcode);
|
||||||
|
return patternLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetMinInstructionLength(string barcode)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < barcode.Length; i++)
|
||||||
|
{
|
||||||
|
var pattern = barcode[0..Math.Min(i+1, barcode.Length)];
|
||||||
|
if(CreatesBarcode([..pattern], barcode))
|
||||||
|
{
|
||||||
|
return pattern.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return barcode.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool CreatesBarcode(char[] pattern, string barcode)
|
||||||
|
{
|
||||||
|
foreach (var barcodeChunk in barcode.Chunk(pattern.Length))
|
||||||
|
{
|
||||||
|
if(TryRepairPattern(pattern, barcodeChunk, out var repairedPattern))
|
||||||
|
pattern = repairedPattern;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryRepairPattern(char[] pattern, char[] barcodeChunk, out char[] repairedPattern)
|
||||||
|
{
|
||||||
|
repairedPattern = pattern;
|
||||||
|
for (var i = 0; i < pattern.Length && i < barcodeChunk.Length; i++)
|
||||||
|
{
|
||||||
|
if(pattern[i] == barcodeChunk[i])
|
||||||
|
continue;
|
||||||
|
if(barcodeChunk[i] == '?')
|
||||||
|
continue;
|
||||||
|
if(pattern[i] == '?')
|
||||||
|
repairedPattern[i] = barcodeChunk[i];
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user