Skip to content

Commit

Permalink
2024 Day14 slight refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 14, 2024
1 parent 7aebfb4 commit b885c3e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
62 changes: 32 additions & 30 deletions Solutions/2024/Day14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,33 @@ public static int Part1(string[] _, Action<string[], bool>? visualise = null, pa
int width = args.TilesWide();
int height = args.TilesTall();

List<Robot> robots = [.. _robots];
List<Robot> robots;

robots.VisualiseMap(width, height, "Initial state:", visualise);
_robots.VisualiseMap(width, height, "Initial state:", visualise);

for (int noOfSeconds = 1; noOfSeconds <= NO_OF_SECONDS; noOfSeconds++) {
robots = [.. robots.Select(r => r.MoveNext(width, height))];
if (noOfSeconds < 6 && height < 50) {
if (height < 50) { // Tests visualisation
robots = [.. _robots];
for (int noOfSeconds = 1; noOfSeconds < 6; noOfSeconds++) {
robots = [.. robots.Select(r => r.MoveNext(width, height))];
robots.VisualiseMap(width, height, $"After {noOfSeconds} seconds:", visualise);
}
}

robots = [.. _robots.Select(r => r.MoveNext(width, height, NO_OF_SECONDS))];

robots.VisualiseMap(width, height, "Final state:", visualise);

return robots.SafetyFactor(width, height);
}

private static Robot MoveNext(this Robot robot, int width, int height)
private static Robot MoveNext(this Robot robot, int width, int height, int noOfSeconds = 1)
{
(int x, int y) = (robot.Position + robot.Velocity);

//if (x >= width ) { x %= width; }
//if (y >= height) { y %= height; }
//if (x < 0) { x = width + x; }
//if (y < 0) { y = height + y; }
(int x, int y) = (robot.Position + (robot.Velocity * noOfSeconds));

x = x < 0 ? width + x : x % width;
y = y < 0 ? height + y : y % height;
if (x < 0) { x = width + (x % width ); }
if (y < 0) { y = height + (y % height); }

return robot with { Position = new(x, y) };
return robot with { Position = new(x % width, y % height) };
}

public static int SafetyFactor(this IEnumerable<Robot> robots, int width, int height)
Expand All @@ -70,53 +68,57 @@ public static int Part2(string[] _, Action<string[], bool>? visualise = null, pa
int width = args.TilesWide();
int height = args.TilesTall();

List<Robot> robots = [.. _robots];
// Doubt any input data will coalesce into a christmas tree in less than 5000 iterations
int noOfSeconds = 5000;

int noOfSeconds = 0;
for (noOfSeconds = 0; !robots.IsChristmasTree(width, height); noOfSeconds++) {
robots = [.. robots.Select(r => r.MoveNext(width, height))];
while (noOfSeconds++ < 50_000
&& !_robots
.Select(r => r.MoveNext(width, height, noOfSeconds))
.IsChristmasTree()) {
}

robots.VisualiseMap(width, height, "Christmas Tree:", visualise);
_robots
.Select(r => r.MoveNext(width, height, noOfSeconds))
.VisualiseMap(width, height, "Christmas Tree:", visualise);

return noOfSeconds;
}

public static bool IsChristmasTree(this IEnumerable<Robot> robots, int width, int _)
public static bool IsChristmasTree(this IEnumerable<Robot> robots)
{
const int THRESHOLD = 10;

int midX = width / 2;

int count = 0;
int prevX = 0;
Point prev = Point.Zero;
foreach (Robot robot in robots.OrderBy(robots => robots.Position)) {
count = (robot.Position.X == prevX + 1)
count = (robot.Position.Y == prev.Y && robot.Position.X == prev.X + 1)
? count + 1
: 0;

if (count > THRESHOLD) {
return true;
}

prevX = robot.Position.X;
prev = robot.Position;
}

return false;
}

public static void VisualiseMap(this IEnumerable<Robot> robots, int width, int height, string title, Action<string[], bool>? visualise)
{
if (visualise is null) {
return;
}

int[,] map = new int[width, height];

foreach (Robot robot in robots) {
map[robot.Position.X, robot.Position.Y] += 1;
}

if (visualise is not null) {
string[] output = ["", title, .. map.AsStrings().Select(s => s.Replace('0', '.'))];
_ = Task.Run(() => visualise?.Invoke(output, false));
}
string[] output = ["", title, .. map.AsStrings().Select(s => s.Replace('0', '.'))];
_ = Task.Run(() => visualise?.Invoke(output, false));
}

private static int TilesWide(this object[]? args) => GetArgument(args, 1, 101);
Expand Down
3 changes: 2 additions & 1 deletion Tests/2024/Tests_14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class Tests_14_Restroom_Redoubt(ITestOutputHelper testOutputHelper)
[InlineData("""
p=2,4 v=2,-3
""", 11, 7, 0)]
public void Part1(string input, int width, int height, int expected)
public async Task Part1(string input, int width, int height, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART1, input, new Action<string[], bool>(Callback), width, height), out int actual);
actual.ShouldBe(expected);
await Task.Delay(200); // Allow time to visualise
}


Expand Down

0 comments on commit b885c3e

Please sign in to comment.