Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var input = await File.ReadAllLinesAsync("input.txt");
- var robots = new List<(int Px, int Py, int Vx, int Vy)>();
- foreach (var line in input)
- {
- var parts = line.Split(' ');
- var pos = parts[0].Split(',');
- var vel = parts[1].Split(',');
- robots.Add((int.Parse(pos[0][2..]), int.Parse(pos[1]), int.Parse(vel[0][2..]), int.Parse(vel[1])));
- }
- const int width = 101;
- const int height = 103;
- var count = 0;
- while (true)
- {
- count++;
- for (var r = 0; r < robots.Count; r++)
- {
- var robot = robots[r];
- var newPosX = (width + (robot.Px + robot.Vx) % width) % width;
- var newPosY = (height + (robot.Py + robot.Vy) % height) % height;
- robots[r] = (newPosX, newPosY, robot.Vx, robot.Vy);
- }
- if (count == 100)
- {
- var topLeft = robots.Count(r => r is { Px: < width / 2, Py: < height / 2 });
- var topRight = robots.Count(r => r is { Px: > width / 2, Py: < height / 2 });
- var bottomLeft = robots.Count(r => r is { Px: < width / 2, Py: > height / 2 });
- var bottomRight = robots.Count(r => r is { Px: > width / 2, Py: > height / 2 });
- Console.WriteLine($"Part 1: {topLeft} * {topRight} * {bottomLeft} * {bottomRight} = {topLeft * topRight * bottomLeft * bottomRight}");
- }
- var positions = robots.Select(r => (r.Px, r.Py)).Distinct().Count();
- if (positions == robots.Count) //No overlapping robots
- {
- Console.WriteLine($"Part 2: {count}");
- Console.WriteLine();
- for (var y = 0; y < height; y++)
- {
- for (var x = 0; x < width; x++)
- {
- Console.Write(robots.Any(r => r.Px == x && r.Py == y) ? '#' : '.');
- }
- Console.WriteLine();
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement