Skip to content

Commit

Permalink
day 14 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 15, 2024
1 parent dfb1532 commit 60113de
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions AdventOfCode/Day14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,31 @@ public override ValueTask<string> Solve_2()
int width = 101;
int height = 103;

//while(true)
//{
// foreach (Robot robot in robots)
// {
// robot.Move(width, height);
// }
//}
int move = 0;

//DisplayRobots(robots, width, height);
while (true)
{
if (MayBeTree(robots, width, height))
{
DisplayRobots(robots, width, height);
Console.WriteLine("Seconds elapsed: " + move);
Console.WriteLine("Is it tree? y/n");
var key = Console.ReadKey();
if (key.KeyChar == 'y')
{
break;
}
}

return new("");
foreach (Robot robot in robots)
{
robot.Move(width, height);
}

move++;
}

return new(move.ToString());
}

private int GetRobotsCount(int xStart, int xEnd, int yStart, int yEnd, List<Robot> robots)
Expand All @@ -58,6 +72,25 @@ private int GetRobotsCount(int xStart, int xEnd, int yStart, int yEnd, List<Robo
&& r.YPosition >= yStart && r.YPosition <= yEnd);
}

private bool MayBeTree(List<Robot> robots, int width, int height)
{
foreach (Robot robot in robots)
{
bool a = robots.Any(r => r.XPosition == robot.XPosition + 1 && r.YPosition == robot.YPosition);
bool b = robots.Any(r => r.XPosition == robot.XPosition && r.YPosition == robot.YPosition + 1);
bool c = robots.Any(r => r.XPosition == robot.XPosition + 1 && r.YPosition == robot.YPosition + 1);
bool d = robots.Any(r => r.XPosition == robot.XPosition && r.YPosition == robot.YPosition + 2);
bool e = robots.Any(r => r.XPosition == robot.XPosition + 1 && r.YPosition == robot.YPosition + 2);

if (a && b && c && d && e)
{
return true;
}
}

return false;
}

private void DisplayRobots(List<Robot> robots, int width, int height)
{
Console.Clear();
Expand Down

0 comments on commit 60113de

Please sign in to comment.