Advertisement
mgla

Advent of Code - 2024 - Day 14

Dec 14th, 2024
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. var input = await File.ReadAllLinesAsync("input.txt");
  2. var robots = new List<(int Px, int Py, int Vx, int Vy)>();
  3.  
  4. foreach (var line in input)
  5. {
  6.     var parts = line.Split(' ');
  7.     var pos = parts[0].Split(',');
  8.     var vel = parts[1].Split(',');
  9.  
  10.     robots.Add((int.Parse(pos[0][2..]), int.Parse(pos[1]), int.Parse(vel[0][2..]), int.Parse(vel[1])));
  11. }
  12.  
  13. const int width = 101;
  14. const int height = 103;
  15. var count = 0;
  16.  
  17. while (true)
  18. {
  19.     count++;
  20.  
  21.     for (var r = 0; r < robots.Count; r++)
  22.     {
  23.         var robot = robots[r];
  24.  
  25.         var newPosX = (width + (robot.Px + robot.Vx) % width) % width;
  26.         var newPosY = (height + (robot.Py + robot.Vy) % height) % height;
  27.  
  28.         robots[r] = (newPosX, newPosY, robot.Vx, robot.Vy);
  29.     }
  30.  
  31.     if (count == 100)
  32.     {
  33.         var topLeft = robots.Count(r => r is { Px: < width / 2, Py: < height / 2 });
  34.         var topRight = robots.Count(r => r is { Px: > width / 2, Py: < height / 2 });
  35.         var bottomLeft = robots.Count(r => r is { Px: < width / 2, Py: > height / 2 });
  36.         var bottomRight = robots.Count(r => r is { Px: > width / 2, Py: > height / 2 });
  37.  
  38.         Console.WriteLine($"Part 1: {topLeft} * {topRight} * {bottomLeft} * {bottomRight} = {topLeft * topRight * bottomLeft * bottomRight}");
  39.     }
  40.  
  41.     var positions = robots.Select(r => (r.Px, r.Py)).Distinct().Count();
  42.  
  43.     if (positions == robots.Count) //No overlapping robots
  44.     {
  45.         Console.WriteLine($"Part 2: {count}");
  46.         Console.WriteLine();
  47.  
  48.         for (var y = 0; y < height; y++)
  49.         {
  50.             for (var x = 0; x < width; x++)
  51.             {
  52.                 Console.Write(robots.Any(r => r.Px == x && r.Py == y) ? '#' : '.');
  53.             }
  54.             Console.WriteLine();
  55.         }
  56.  
  57.         break;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement