public void MakeVaporizeList(int maxBlasts, out int lastX, out int lastY) { // as we vaporize asteroids, remove them from here var vaporizedMap = new string[rawMap.Length]; rawMap.CopyTo(vaporizedMap, 0); int x = 0; int y = 0; VisibleAsteroidsFrom best = new VisibleAsteroidsFrom(); GetMostVisibile(out best); x = best.x; y = best.y; // each time we vaporize an asteroid, add the x,y into here List <Tuple <int, int> > vaporedList = new List <Tuple <int, int> >(); // if we blast an asteroid, set this to true as a safety bool blasted = false; while (vaporedList.Count < maxBlasts) { // keep track of places we've visited and the rays that extend through it, but reset on each loop (360 deg) Dictionary <Tuple <int, int>, bool> visited = new Dictionary <Tuple <int, int>, bool>(); // go right & up for (int skipX = 0; skipX < (maxX - x); skipX++) { for (int skipY = 0; skipY >= (-1 * y); skipY--) { bool foundAster = false; BlastThem(x, y, skipX, skipY, visited, vaporizedMap, vaporedList, ref foundAster); blasted |= foundAster; } } // for down & right for (int skipY = 0; skipY < (maxY - y); skipY++) { for (int skipX = 0; skipX < (maxX - x); skipX++) { bool foundAster = false; BlastThem(x, y, skipX, skipY, visited, vaporizedMap, vaporedList, ref foundAster); blasted |= foundAster; } } // going left & down for (int skipX = 0; skipX >= (-1 * x); skipX--) { for (int skipY = 0; skipY < (maxY - y); skipY++) { bool foundAster = false; BlastThem(x, y, skipX, skipY, visited, vaporizedMap, vaporedList, ref foundAster); blasted |= foundAster; } } // going left & up for (int skipY = 0; skipY >= (-1 * y); skipY--) { for (int skipX = 0; skipX >= (-1 * x); skipX--) { bool foundAster = false; BlastThem(x, y, skipX, skipY, visited, vaporizedMap, vaporedList, ref foundAster); blasted |= foundAster; } } if (!blasted) { break; } // else reset blasted = false; } lastX = -1; lastY = -1; if (vaporedList.Count >= maxBlasts) { lastX = vaporedList[maxBlasts - 1].Item1; lastY = vaporedList[maxBlasts - 1].Item2; } }
public void FindSightLines() { Parallel.For(0, maxY, y => { // or do in parallel // for each x position for (int x = 0; x < maxX; x++) { // for each location // is this position an asteroid? if (rawMap[y][x] == '#') { // if so, then look for all the asteroids that it can see Dictionary <Tuple <int, int>, bool> visited = new Dictionary <Tuple <int, int>, bool>(); // going up from point for (int skipY = 0; skipY >= (-1 * y); skipY--) { // going right from point for (int skipX = 0; skipX < (maxX - x); skipX++) { SearchMultipliers(x, y, skipX, skipY, ref visited); } // going left from point for (int skipX = 0; skipX >= (-1 * x); skipX--) { SearchMultipliers(x, y, skipX, skipY, ref visited); } } // going down from the point for (int skipY = 0; skipY < (maxY - y); skipY++) { // going right from point for (int skipX = 0; skipX < (maxX - x); skipX++) { SearchMultipliers(x, y, skipX, skipY, ref visited); } // going left from point for (int skipX = 0; skipX >= (-1 * x); skipX--) { SearchMultipliers(x, y, skipX, skipY, ref visited); } } // add to bag var vaf = new VisibleAsteroidsFrom() { numAsteroidsVisible = asteroids_in_space[y, x] }; vaf.x = x; vaf.y = y; visiblesList.Add(vaf); } } }); }
public void GetMostVisibile(out VisibleAsteroidsFrom best) { best = visiblesList.OrderBy(x => x.numAsteroidsVisible).Last(); }