//For Debugging //private static void DrawMap(Dist[,] map) //{ // var width = map.GetLength(0); // var height = map.GetLength(1); // for (var x = 0; x < width; x++) // { // for (var y = 0; y < height; y++) // { // Console.Write($"{map[x, y].Id}"); // } // Console.WriteLine(); // } //} private static (char, int) FillMap(Dist[,] map, Dictionary <char, Point> points) { /*Naive initial impl. went points first, and then multiple iterations of the map */ var width = map.GetLength(0); var height = map.GetLength(1); foreach (var p in points.Values) { for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var d = GetManhattanDistance(p, x, y); var isInf = IsInfinite(x, y, width, height); if ((map[x, y] == null) || //unmapped (map[x, y].Id != p.Id && map[x, y].D > d)) //someone else further away { map[x, y] = new Dist { Id = p.Id, D = d, IsInfinite = isInf }; } else if (map[x, y].Id != p.Id && map[x, y].D == d) { map[x, y] = new Dist { Id = '.', D = d, IsInfinite = isInf }; } } } } //Flatten It var tmp = new Dist[width * height]; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { tmp[y * width + x] = map[x, y]; } } //Group By Cell Id & coun't the ones that are not touching infinity var r = tmp.GroupBy(k => k.Id) .Where(w => !w.Any(d => d.IsInfinite)) .Select(s => new { Id = s.Key, Count = s.Count() }) .OrderByDescending(o => o.Count) .First(); return(r.Id, r.Count); }