diff --git a/14/RestroomRedoubt.Tests/SimulationTests.cs b/14/RestroomRedoubt.Tests/SimulationTests.cs index 3e63b90..0d576a4 100644 --- a/14/RestroomRedoubt.Tests/SimulationTests.cs +++ b/14/RestroomRedoubt.Tests/SimulationTests.cs @@ -23,20 +23,37 @@ public class SimulationTests } [Test] - public async Task Run_WhenCalledWithPuzzleInput_ItShouldReturnExpectedValue() + public async Task Run_WhenCalledWithPuzzleInput_ItShouldReturnPartOneSolution() { var input = await GetPuzzleInput(); - - var result = Simulation.From(101, 103, input).Run(100); - await Assert.That(result).IsEqualTo(224554908); + var simulation = Simulation.From(101, 103, input); + simulation.Run(100); + + await Assert.That(simulation.SafetyFactor).IsEqualTo(224554908); + } + + [Test] + public async Task Run_WhenCalledWithPuzzleInput_ItShouldReturnPartTwoSolution() + { + var input = await GetPuzzleInput(); + + var width = 101; + var height = 103; + var times = width * height; + + var result = Simulation.From(width, height, input).Run(times).MinBy(kvp => kvp.Value).Key; + + await Assert.That(result).IsEqualTo(0); } [Test] public async Task Run_WhenCalledWithExampleInput_ItShouldReturnExpectedValue() { - var result = Simulation.From(11, 7, _exampleInput).Run(100); + var simulation = Simulation.From(11, 7, _exampleInput); - await Assert.That(result).IsEqualTo(12); + simulation.Run(100); + + await Assert.That(simulation.SafetyFactor).IsEqualTo(12); } } \ No newline at end of file diff --git a/14/RestroomRedoubt/Program.cs b/14/RestroomRedoubt/Program.cs index c042efc..e493119 100644 --- a/14/RestroomRedoubt/Program.cs +++ b/14/RestroomRedoubt/Program.cs @@ -17,13 +17,34 @@ if (File.Exists(args[0]) is false) var isPart2 = args.Length is 2 && args[1] is "part2"; var input = await File.ReadAllLinesAsync(args[0]); +// TODO: This still doesn't feel like the right API tho...even though it does solve var stopwatch = new Stopwatch(); stopwatch.Start(); -var result = Simulation.From(101, 103, input).Run(101 * 103); +var width = 101; +var height = 103; +var times = isPart2 + ? width * height + : 100; + +var simulation = Simulation.From(width, height, input); +var simulationResults = simulation.Run(times); +var result = isPart2 + ? simulationResults.MinBy(kvp => kvp.Value).Key + : simulation.SafetyFactor; + +var msg = isPart2 + ? $"The Christmas tree appears after {result} seconds" + : $"The safety factor will be {result}"; stopwatch.Stop(); -Console.WriteLine($"The safety factor will be {result}. ({stopwatch.ElapsedMilliseconds}ms)"); +Console.WriteLine($"{msg}. ({stopwatch.ElapsedMilliseconds}ms)"); + +if (isPart2) +{ + simulation.Run(result); + simulation.PrintToConsole(); +} class Simulation { @@ -33,7 +54,7 @@ class Simulation private readonly List _quadrants; private List RobotsPerQuadrant => _quadrants.Select(quadrant => _robots.Count(r => r.IsIn(quadrant))).ToList(); - private int SafetyFactor => RobotsPerQuadrant.Aggregate(1, (current, count) => current * count); + public int SafetyFactor => RobotsPerQuadrant.Aggregate(1, (current, count) => current * count); private Simulation(int width, int height, List robots) { @@ -52,18 +73,22 @@ class Simulation } public static Simulation From(int width, int height, string[] input) => new(width, height, input.Select(Robot.From).ToList()); - - public int Run(int times) + + public Dictionary Run(int times) { + var safetyFactorMap = new Dictionary(); + for (int i = 0; i < times; i++) { foreach (var robot in _robots) { robot.Move(_width, _height); } + + safetyFactorMap.Add(i + 1, SafetyFactor); } - - return SafetyFactor; + + return safetyFactorMap; } public override string ToString() diff --git a/README.md b/README.md index e823cb6..ef25377 100644 --- a/README.md +++ b/README.md @@ -57,3 +57,4 @@ dotnet build | 11 | [Problem](./11/PROBLEM.md) | [Solution](./11/PlutonianPebbles/) | Dictionary > List | | 12 | [Problem](./12/PROBLEM.md) | [Solution](./12/GardenGroups/) | CORNERS == SIDES | | 13 | [Problem](./13/PROBLEM.md) | [Solution](./13/ClawContraption/) | Yay for algebra | +| 14 | [PROBLEM](./14/PROBLEM.md) | [Solution](./14/RestroomRedoubt/) | Part 2 is not as complicated as you think. The answer is using the safety factor |