From ca078ba6d3e2f3268d13351558532bb2b1e4ecfd Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 15 Dec 2024 14:36:40 -0600 Subject: [PATCH] chore: cleanup --- 14/RestroomRedoubt/Program.cs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/14/RestroomRedoubt/Program.cs b/14/RestroomRedoubt/Program.cs index 21596dd..c042efc 100644 --- a/14/RestroomRedoubt/Program.cs +++ b/14/RestroomRedoubt/Program.cs @@ -20,7 +20,7 @@ var input = await File.ReadAllLinesAsync(args[0]); var stopwatch = new Stopwatch(); stopwatch.Start(); -var result = Simulation.From(101, 103, input).Run(100); +var result = Simulation.From(101, 103, input).Run(101 * 103); stopwatch.Stop(); Console.WriteLine($"The safety factor will be {result}. ({stopwatch.ElapsedMilliseconds}ms)"); @@ -31,6 +31,9 @@ class Simulation private readonly int _width; private readonly int _height; 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); private Simulation(int width, int height, List robots) { @@ -58,13 +61,9 @@ class Simulation { robot.Move(_width, _height); } - - // Debug(i); } - - return _quadrants - .Select(quadrant => _robots.Count(r => r.IsIn(quadrant))) - .Aggregate(1, (current, count) => current * count); + + return SafetyFactor; } public override string ToString() @@ -77,14 +76,7 @@ class Simulation for (int currentRow = 0; currentRow < _height; currentRow++) { - if (_robots.Any(r => r.IsIn(currentColumn, currentRow))) - { - row.Append('R'); - } - else - { - row.Append('.'); - } + row.Append(_robots.Any(r => r.IsIn(currentColumn, currentRow)) ? 'R' : '.'); } lines.AppendLine(row.ToString()); @@ -92,12 +84,17 @@ class Simulation return lines.ToString(); } + + public void PrintToConsole() => Console.WriteLine(ToString()); + + public Task PrintToFileAsync(string filename) => + File.WriteAllTextAsync(Path.Combine(AppContext.BaseDirectory, filename), ToString()); } partial record Robot { - private int PositionX { get; set; } - private int PositionY { get; set; } + public int PositionX { get; private set; } + public int PositionY { get; private set; } private int VelocityX { get; } private int VelocityY { get; }