feat: solved day 3

This commit is contained in:
Stevan Freeborn
2024-12-03 23:50:01 -06:00
parent 0a7b6b2291
commit d658c31ffe
9 changed files with 225 additions and 15 deletions
@@ -0,0 +1,38 @@
namespace MullItOver.Tests;
public class InstructionExtensionsTests
{
[Test]
public async Task Execute_WhenCalledWithNoConditionals_ItShouldReturnCorrectSum()
{
var instructions = new List<Instruction>()
{
new MulInstruction(2, 4),
new MulInstruction(5, 5),
new MulInstruction(11, 8),
new MulInstruction(8, 5),
};
var result = instructions.Execute();
await Assert.That(result).IsEqualTo(161);
}
[Test]
public async Task Execute_WhenCalledWithConditionals_ItShouldReturnCorrectSum()
{
var instructions = new List<Instruction>()
{
new MulInstruction(2, 4),
new DontInstruction(),
new MulInstruction(5, 5),
new MulInstruction(11, 8),
new DoInstruction(),
new MulInstruction(8, 5),
};
var result = instructions.Execute();
await Assert.That(result).IsEqualTo(48);
}
}
+12
View File
@@ -0,0 +1,12 @@
namespace MullItOver.Tests;
public class InstructionTests
{
[Test]
public async Task Execute_WhenCalled_ItShouldReturnProductOfMultiplicands()
{
var result = new MulInstruction(2, 2).Execute();
await Assert.That(result).IsEquivalentTo(4);
}
}
+30
View File
@@ -0,0 +1,30 @@
namespace MullItOver.Tests;
public class PuzzleInputTests
{
private readonly PuzzleParser _parser = new();
private async Task<string> GetPuzzleInput()
{
var inputPath = Path.Combine(AppContext.BaseDirectory, "INPUT.txt");
return await File.ReadAllTextAsync(inputPath);
}
[Test]
public async Task Solve_WhenGivenInput_ItShouldSolvePartOne()
{
var input = await GetPuzzleInput();
var result = _parser.Parse(input).Execute();
await Assert.That(result).IsEqualTo(174960292);
}
[Test]
public async Task Solve_WhenGivenInput_ItShouldSolvePartTwo()
{
var input = await GetPuzzleInput();
var result = _parser.ParseWithConditionals(input).Execute();
await Assert.That(result).IsEqualTo(56275602);
}
}
+40
View File
@@ -3,6 +3,8 @@
public class PuzzleParserTests
{
private readonly PuzzleParser _parser = new();
private const string TestInput = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
private const string Part2TestInput = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
[Test]
public async Task Parse_WhenInputIsEmpty_ReturnsEmptyList()
@@ -11,4 +13,42 @@ public class PuzzleParserTests
await Assert.That(result).IsEquivalentTo(new List<Instruction>());
}
[Test]
public async Task Parse_WhenGivenSetOfMalformedInstructions_ItShouldReturnListOfActualInstructions()
{
var result = _parser.Parse(TestInput);
await Assert.That(result).IsEquivalentTo(new List<Instruction>()
{
new MulInstruction(2, 4),
new MulInstruction(5, 5),
new MulInstruction(11, 8),
new MulInstruction(8, 5),
});
}
[Test]
public async Task ParseWithConditionals_WhenInputIsEmpty_ReturnsEmptyList()
{
var result = _parser.ParseWithConditionals("");
await Assert.That(result).IsEquivalentTo(new List<Instruction>());
}
[Test]
public async Task ParseWithConditionals_WhenGivenSetOfMalformedInstructionsItShouldReturnListOfActualInstructions()
{
var result = _parser.ParseWithConditionals(Part2TestInput);
await Assert.That(result).IsEquivalentTo(new List<Instruction>()
{
new MulInstruction(2, 4),
new DontInstruction(),
new MulInstruction(5, 5),
new MulInstruction(11, 8),
new DoInstruction(),
new MulInstruction(8, 5),
});
}
}