feat: solved day 3
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace MullItOver;
|
||||
|
||||
public abstract class Instruction {};
|
||||
|
||||
class MulInstruction(int multiplicandOne, int multiplicandTwo) : Instruction
|
||||
{
|
||||
public int Execute() => multiplicandOne * multiplicandTwo;
|
||||
}
|
||||
|
||||
class DontInstruction : Instruction;
|
||||
|
||||
class DoInstruction : Instruction;
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace MullItOver;
|
||||
|
||||
public static class InstructionExtensions
|
||||
{
|
||||
public static int Execute(this List<Instruction> instructions)
|
||||
{
|
||||
var isEnabled = true;
|
||||
var sum = 0;
|
||||
|
||||
foreach (var instruction in instructions)
|
||||
{
|
||||
switch (instruction)
|
||||
{
|
||||
case DontInstruction:
|
||||
isEnabled = false;
|
||||
continue;
|
||||
case DoInstruction:
|
||||
isEnabled = true;
|
||||
continue;
|
||||
case MulInstruction mul when isEnabled:
|
||||
sum += mul.Execute();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using MullItOver;
|
||||
|
||||
if (args.Length is 0)
|
||||
{
|
||||
Console.WriteLine("Please provide a path to the input file.");
|
||||
@@ -18,21 +20,13 @@ var input = await File.ReadAllTextAsync(args[0]);
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var instructions = new PuzzleParser().Parse(input);
|
||||
var parser = new PuzzleParser();
|
||||
|
||||
var results = 0;
|
||||
var instructions = isPart2
|
||||
? parser.ParseWithConditionals(input)
|
||||
: parser.Parse(input);
|
||||
|
||||
var results = instructions.Execute();
|
||||
|
||||
stopwatch.Stop();
|
||||
Console.WriteLine($"The sum of instructions is {results}. ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
|
||||
class PuzzleParser
|
||||
{
|
||||
public List<Instruction> Parse(string input)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
class Instruction(int multiplicandOne, int multiplicandTwo)
|
||||
{
|
||||
}
|
||||
Console.WriteLine($"The sum of instructions is {results}. ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
@@ -0,0 +1,55 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MullItOver;
|
||||
|
||||
partial class PuzzleParser
|
||||
{
|
||||
[GeneratedRegex(@"mul\((\d{1,3}),(\d{1,3})\)")]
|
||||
private static partial Regex InstructionsRegex();
|
||||
|
||||
[GeneratedRegex(@"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)")]
|
||||
private static partial Regex InstructionsWithConditionalsRegex();
|
||||
|
||||
public List<Instruction> Parse(string input)
|
||||
{
|
||||
var matches = InstructionsRegex().Matches(input);
|
||||
return matches
|
||||
.Select(Instruction (m) => new MulInstruction(
|
||||
int.Parse(m.Groups[1].Value),
|
||||
int.Parse(m.Groups[2].Value)
|
||||
))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<Instruction> ParseWithConditionals(string input)
|
||||
{
|
||||
var instructions = new List<Instruction>();
|
||||
var matches = InstructionsWithConditionalsRegex()
|
||||
.Matches(input)
|
||||
.ToList();
|
||||
|
||||
foreach (var match in matches)
|
||||
{
|
||||
var firstGroup = match.Groups[0].Value;
|
||||
|
||||
if (firstGroup.Contains("mul"))
|
||||
{
|
||||
instructions.Add(new MulInstruction(
|
||||
int.Parse(match.Groups[1].Value),
|
||||
int.Parse(match.Groups[2].Value)
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (firstGroup.Contains('\''))
|
||||
{
|
||||
instructions.Add(new DontInstruction());
|
||||
continue;
|
||||
}
|
||||
|
||||
instructions.Add(new DoInstruction());
|
||||
}
|
||||
|
||||
return instructions;
|
||||
}
|
||||
}
|
||||
@@ -46,3 +46,4 @@ dotnet build
|
||||
|-----|----------------------------|:-----------------------------------:|----------------------------------------------------------------------------------------------|
|
||||
| 01 | [Problem](./01/PROBLEM.md) | [Solution](./01/HistorianHysteria/) | A great way to start! |
|
||||
| 02 | [Problem](./02/PROBLEM.md) | [Solution](./02/RedNosedReports/) | Damn their was an edge case that really bit me...direction change after first pair of levels |
|
||||
| 03 | [Problem](./03/PROBLEM.md) | [Solution](./03/MullItOver/) | Thank goodness this wasn't a repeat of day 2. |
|
||||
|
||||
Reference in New Issue
Block a user