feat: solve day 5 part 1

This commit is contained in:
Stevan Freeborn
2024-12-05 23:10:08 -06:00
parent d8f67e85ca
commit a9f9dec7aa
9 changed files with 292 additions and 6 deletions
+76
View File
@@ -0,0 +1,76 @@
namespace PrintQueue.Tests;
public class PuzzleParserTests
{
private readonly PuzzleParser _puzzleParser = new();
private readonly string _exampleInput = $"47|53{Environment.NewLine}97|13{Environment.NewLine}97|61{Environment.NewLine}97|47{Environment.NewLine}75|29{Environment.NewLine}61|13{Environment.NewLine}75|53{Environment.NewLine}29|13{Environment.NewLine}97|29{Environment.NewLine}53|29{Environment.NewLine}61|53{Environment.NewLine}97|53{Environment.NewLine}61|29{Environment.NewLine}47|13{Environment.NewLine}75|47{Environment.NewLine}97|75{Environment.NewLine}47|61{Environment.NewLine}75|61{Environment.NewLine}47|29{Environment.NewLine}75|13{Environment.NewLine}53|13{Environment.NewLine}{Environment.NewLine}75,47,61,53,29{Environment.NewLine}97,61,53,29,13{Environment.NewLine}75,29,13{Environment.NewLine}75,97,47,61,53{Environment.NewLine}61,13,29{Environment.NewLine}97,13,75,29,47";
private static async Task<string> GetPuzzleInput()
{
return await File.ReadAllTextAsync(Path.Combine(AppContext.BaseDirectory, "INPUT.txt"));
}
[Test]
public async Task Parse_WhenGivenExampleInput_ItShouldReturnExpectedRules()
{
var result = _puzzleParser.Parse(_exampleInput);
await Assert.That(result.Rules).IsEquivalentTo(new List<OrderRule>()
{
new(47, 53),
new(97, 13),
new(97, 61),
new(97, 47),
new(75, 29),
new(61, 13),
new(75, 53),
new(29, 13),
new(97, 29),
new(53, 29),
new(61, 53),
new(97, 53),
new(61, 29),
new(47, 13),
new(75, 47),
new(97, 75),
new(47, 61),
new(75, 61),
new(47, 29),
new(75, 13),
new(53, 13),
});
}
[Test]
public async Task Parse_WhenGivenExampleInput_ItShouldReturnExpectedUpdates()
{
var result = _puzzleParser.Parse(_exampleInput);
await Assert.That(result.Updates).IsEquivalentTo(new List<Update>()
{
new([75,47,61,53,29]),
new([97,61,53,29,13]),
new([75,29,13]),
new([75,97,47,61,53]),
new([61,13,29]),
new([97,13,75,29,47]),
});
}
[Test]
public async Task Parse_WhenGivenPuzzleInput_ItShouldReturnExpectedResult()
{
var input = await GetPuzzleInput();
var parseResult = _puzzleParser.Parse(input);
var validator = new UpdateValidator(parseResult.Rules);
var result = parseResult.Updates
.Where(u => validator.Validate(u))
.Select(u => u.GetMiddlePage())
.Sum();
await Assert.That(result).IsEqualTo(6260);
}
}
+19
View File
@@ -0,0 +1,19 @@
namespace PrintQueue.Tests;
public class UpdateTests
{
[Test]
public async Task GetMiddlePage_WhenCalled_ItShouldReturnExpectedPage()
{
var updates = new List<Update>()
{
new([75,47,61,53,29]),
new([97,61,53,29,13]),
new([75,29,13]),
};
var result = updates.Select(u => u.GetMiddlePage()).Sum();
await Assert.That(result).IsEqualTo(143);
}
}
@@ -0,0 +1,84 @@
namespace PrintQueue.Tests;
public class UpdateValidatorTests
{
private readonly List<OrderRule> _exampleRules = [
new(47, 53),
new(97, 13),
new(97, 61),
new(97, 47),
new(75, 29),
new(61, 13),
new(75, 53),
new(29, 13),
new(97, 29),
new(53, 29),
new(61, 53),
new(97, 53),
new(61, 29),
new(47, 13),
new(75, 47),
new(97, 75),
new(47, 61),
new(75, 61),
new(47, 29),
new(75, 13),
new(53, 13),
];
[Test]
public async Task Graph_WhenGivenExampleRules_ItShouldBuildExpectedGraph()
{
var expectedGraph = new Dictionary<int, HashSet<int>>()
{
{ 47, [53, 13, 61, 29] },
{ 53, [29, 13] },
{ 97, [13, 61, 47, 29, 53, 75] },
{ 13, [] },
{ 61, [13, 53, 29] },
{ 75, [29, 53, 47, 61, 13] },
{ 29, [13] },
};
var validator = new UpdateValidator(_exampleRules);
await Assert.That(validator.Graph.Keys).IsEquivalentTo(expectedGraph.Keys);
await Assert.That(validator.Graph.Values).IsEquivalentTo(expectedGraph.Values);
}
[Test]
public async Task Validate_WhenGivenValidUpdate_ItShouldReturnTrue()
{
var updateOne = new Update([75, 47, 61, 53, 29]);
var updateTwo = new Update([97,61,53,29,13]);
var updateThree = new Update([75,29,13]);
var validator = new UpdateValidator(_exampleRules);
var resultOne = validator.Validate(updateOne);
var resultTwo = validator.Validate(updateTwo);
var resultThree = validator.Validate(updateThree);
await Assert.That(resultOne).IsTrue();
await Assert.That(resultTwo).IsTrue();
await Assert.That(resultThree).IsTrue();
}
[Test]
public async Task Validate_WhenGivenInvalidUpdate_ItShouldReturnFalse()
{
var updateOne = new Update([75,97,47,61,53]);
var updateTwo = new Update([61,13,29]);
var updateThree = new Update([97,13,75,29,47]);
var validator = new UpdateValidator(_exampleRules);
var resultOne = validator.Validate(updateOne);
var resultTwo = validator.Validate(updateTwo);
var resultThree = validator.Validate(updateThree);
await Assert.That(resultOne).IsFalse();
await Assert.That(resultTwo).IsFalse();
await Assert.That(resultThree).IsFalse();
}
}