chore: cleanup

This commit is contained in:
Stevan Freeborn
2024-12-15 14:36:40 -06:00
parent 713aec5bc3
commit ca078ba6d3
+13 -16
View File
@@ -20,7 +20,7 @@ var input = await File.ReadAllLinesAsync(args[0]);
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
var result = Simulation.From(101, 103, input).Run(100); var result = Simulation.From(101, 103, input).Run(101 * 103);
stopwatch.Stop(); stopwatch.Stop();
Console.WriteLine($"The safety factor will be {result}. ({stopwatch.ElapsedMilliseconds}ms)"); Console.WriteLine($"The safety factor will be {result}. ({stopwatch.ElapsedMilliseconds}ms)");
@@ -31,6 +31,9 @@ class Simulation
private readonly int _width; private readonly int _width;
private readonly int _height; private readonly int _height;
private readonly List<Quadrant> _quadrants; private readonly List<Quadrant> _quadrants;
private List<int> 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<Robot> robots) private Simulation(int width, int height, List<Robot> robots)
{ {
@@ -58,13 +61,9 @@ class Simulation
{ {
robot.Move(_width, _height); robot.Move(_width, _height);
} }
// Debug(i);
} }
return _quadrants return SafetyFactor;
.Select(quadrant => _robots.Count(r => r.IsIn(quadrant)))
.Aggregate(1, (current, count) => current * count);
} }
public override string ToString() public override string ToString()
@@ -77,14 +76,7 @@ class Simulation
for (int currentRow = 0; currentRow < _height; currentRow++) for (int currentRow = 0; currentRow < _height; currentRow++)
{ {
if (_robots.Any(r => r.IsIn(currentColumn, currentRow))) row.Append(_robots.Any(r => r.IsIn(currentColumn, currentRow)) ? 'R' : '.');
{
row.Append('R');
}
else
{
row.Append('.');
}
} }
lines.AppendLine(row.ToString()); lines.AppendLine(row.ToString());
@@ -92,12 +84,17 @@ class Simulation
return lines.ToString(); 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 partial record Robot
{ {
private int PositionX { get; set; } public int PositionX { get; private set; }
private int PositionY { get; set; } public int PositionY { get; private set; }
private int VelocityX { get; } private int VelocityX { get; }
private int VelocityY { get; } private int VelocityY { get; }