private int Part2GeneralFunction(AsteroidGrid asteroids, AsteroidGrid bestSolution) { asteroids.PrintGrid(); if (asteroids.AsteroidCount < 200) { return(0); } asteroids.SetBestLocation(false); var sorted = new List <SlopedLocation>(); for (int x = 0; x < asteroids.Width; x++) { for (int y = 0; y < asteroids.Height; y++) { if (asteroids[x, y]) { var location = new Location2D(x, y); var degrees = AddDegrees(asteroids.BestLocation.GetSlopeDegrees(location), -90); var slopedLocation = new SlopedLocation(location, degrees, asteroids.BestLocation); sorted.Add(slopedLocation); } } } sorted.Sort(); int consecutive = 1; for (int i = 1; i < sorted.Count; i++) { if (sorted[i].HasEqualAbsoluteAngle(sorted[i - 1])) { sorted[i].AddFullCircleRotations(consecutive++); } else { consecutive = 1; } } sorted.Sort(); var l = sorted[199].Location; return(l.X * 100 + l.Y); }
private int General(GeneralFunction generalFunction) { var lines = FileLines; int height = lines.Length; int width = lines[0].Length; var asteroids = new AsteroidGrid(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { asteroids[x, y] = lines[y][x] == '#'; } } var bestSolution = new AsteroidGrid(0, 0); Location2D bestLocation = (0, 0); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (!asteroids[x, y]) { continue; } var currentlyVisibleAsteroids = new AsteroidGrid(asteroids); currentlyVisibleAsteroids[x, y] = false; for (int x0 = 0; x0 < width; x0++) { for (int y0 = 0; y0 < height; y0++) { if (!currentlyVisibleAsteroids[x0, y0]) { continue; } int xDelta = x0 - x; int yDelta = y0 - y; SimplifyFraction(ref xDelta, ref yDelta); int multiplier = 1; int x1, y1; bool foundFirst = false; while (IsValidIndex(x1 = x + multiplier * xDelta, width) && IsValidIndex(y1 = y + multiplier * yDelta, height)) { if (foundFirst && currentlyVisibleAsteroids[x1, y1]) { currentlyVisibleAsteroids[x1, y1] = false; } foundFirst |= currentlyVisibleAsteroids[x1, y1]; multiplier++; } } } if (currentlyVisibleAsteroids.AsteroidCount > bestSolution.AsteroidCount) { bestSolution = currentlyVisibleAsteroids; bestLocation = (x, y); } } } bestSolution.BestLocation = asteroids.BestLocation = bestLocation; return(generalFunction(asteroids, bestSolution)); }
public AsteroidGrid(AsteroidGrid other) : base(other) { }
private int Part1GeneralFunction(AsteroidGrid asteroids, AsteroidGrid bestSolution) { bestSolution.PrintGrid(); return(bestSolution.AsteroidCount); }