Skip to content

Commit

Permalink
2024 Day14 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 14, 2024
1 parent 83462b6 commit 70120fe
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions Solutions/2024/Day14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static partial class Day14 {
[Init]
public static void LoadRobots(string[] input) => _robots = [.. input.As<Robot>()];


public static int Part1(string[] _, Action<string[], bool>? visualise = null, params object[]? args)
{
const int NO_OF_SECONDS = 100;
Expand All @@ -20,12 +21,12 @@ public static int Part1(string[] _, Action<string[], bool>? visualise = null, pa
int height = args.TilesTall();

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

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) {
if (noOfSeconds < 6 && height < 50) {
robots.VisualiseMap(width, height, $"After {noOfSeconds} seconds:", visualise);
}
}
Expand All @@ -35,50 +36,92 @@ public static int Part1(string[] _, Action<string[], bool>? visualise = null, pa
return robots.SafetyFactor(width, height);
}

public static string Part2(string[] input, params object[]? args) => NO_SOLUTION_WRITTEN_MESSAGE;

private static Robot MoveNext(this Robot robot, int width, int height)
{
(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; }

//if (x >= width ) { x %= width; }
//if (y >= height) { y %= height; }
//if (x < 0) { x = width + x; }
//if (y < 0) { y = height + y; }

x = x < 0 ? width + x : x % width;
y = y < 0 ? height + y : y % height;

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

public static int SafetyFactor(this IEnumerable<Robot> robots, int width, int height)
{
int midX = width / 2;
int midX = width / 2;
int midY = height / 2;

int quad1 = robots.Count(robot => robot.Position.X < midX && robot.Position.Y < midY);
int quad2 = robots.Count(robot => robot.Position.X > midX && robot.Position.Y < midY);
int quad3 = robots.Count(robot => robot.Position.X < midX && robot.Position.Y > midY);
int quad4 = robots.Count(robot => robot.Position.X > midX && robot.Position.Y > midY);
int quad1 = robots.Count(robot => robot.Position.X < midX && robot.Position.Y < midY);
int quad2 = robots.Count(robot => robot.Position.X > midX && robot.Position.Y < midY);
int quad3 = robots.Count(robot => robot.Position.X < midX && robot.Position.Y > midY);
int quad4 = robots.Count(robot => robot.Position.X > midX && robot.Position.Y > midY);

return quad1 * quad2 * quad3 * quad4;
}


public static int Part2(string[] _, Action<string[], bool>? visualise = null, params object[]? args)
{
int width = args.TilesWide();
int height = args.TilesTall();

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

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

robots.VisualiseMap(width, height, "Christmas Tree:", visualise);

return noOfSeconds;
}

public static void VisualiseMap(this IEnumerable<Robot> robots, int width, int height, string title, Action<string[], bool>? visualise)
public static bool IsChristmasTree(this IEnumerable<Robot> robots, int width, int _)
{
const char EMPTY = '.';
int midX = width / 2;

int count = 0;
int prevX = 0;
foreach (Robot robot in robots.OrderBy(robots => robots.Position.Y).ThenBy(robots => robots.Position.X)) {
if (robot.Position.X == prevX + 1 ) {
count++;
if (count > 10) {
return true;
}
} else {
count = 0;
}

prevX = robot.Position.X;
}

return false;
}

public static void VisualiseMap(this IEnumerable<Robot> robots, int width, int height, string title, Action<string[], bool>? visualise)
{
int[,] map = new int[width, height];
//map = map.Fill(0);

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', EMPTY))];
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);
private static int TilesTall(this object[]? args) => GetArgument(args, 2, 103);


public sealed record Robot(Point Position, Point Velocity) : IParsable<Robot>
{
public static Robot Parse(string s, IFormatProvider? provider)
Expand All @@ -97,8 +140,4 @@ public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? prov

[GeneratedRegex("""p=(?<positionx>[\+\-]?\d+),(?<positiony>[\+\-]?\d+) v=(?<velocityx>[\+\-]?\d+),(?<velocityy>[\+\-]?\d+)""")]
public static partial Regex InputRegEx();

private static int TilesWide(this object[]? args) => GetArgument(args, 1, 101);
private static int TilesTall(this object[]? args) => GetArgument(args, 2, 103);

}

0 comments on commit 70120fe

Please sign in to comment.