Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. var bots = input.Split(new string[] { "\n", "pos=<", ",", ">", ", ", " ", "r=", "\n" }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
  2.             var nanoBots = new List<NanoBots>();
  3.             var bot = new NanoBots();
  4.            
  5.             long distance = 1;
  6.             for (var i = 0; i < bots.Count(); i += 4)
  7.             {
  8.                 nanoBots.Add(new NanoBots
  9.                 {
  10.                     Pos = (bots[i], bots[i + 1], bots[i + 2]),
  11.                     Radius = bots[i + 3]
  12.                 });
  13.             }
  14.             var xs = new List<long>();
  15.             var ys = new List<long>();
  16.             var zs = new List<long>();
  17.             foreach(var nanoBot in nanoBots)
  18.             {
  19.                 xs.Add(nanoBot.Pos.x);
  20.                 ys.Add(nanoBot.Pos.y);
  21.                 zs.Add(nanoBot.Pos.z);
  22.             }
  23.             xs.Add(0);
  24.             ys.Add(0);
  25.             zs.Add(0);
  26.  
  27.             while(distance < xs.Max() - xs.Min())
  28.             {
  29.                 distance *= 2;
  30.             }
  31.             while(true)
  32.             {
  33.                 var targetCount = 0;
  34.                 var bestBot = (x: (long)0, y: (long)0, z: (long)0);
  35.                 long maxBot = 0;
  36.                 for(var x = xs.Min(); x < xs.Max() + 1; x+=distance)
  37.                 {
  38.                     for(var y = ys.Min(); y < ys.Max() + 1; y+=distance)
  39.                     {
  40.                         for(var z = zs.Min(); z < zs.Max() + 1; z+=distance)
  41.                         {
  42.                             var count = 0;
  43.                             foreach(var nanoBot in nanoBots)
  44.                             {
  45.                                 var calc = Math.Abs(x - nanoBot.Pos.x) + Math.Abs(y - nanoBot.Pos.y) + Math.Abs(z - nanoBot.Pos.z);
  46.                                 if(((calc - nanoBot.Radius) / distance) <= 0)
  47.                                 {
  48.                                     count++;
  49.                                 }
  50.                             }
  51.                             if(count > targetCount)
  52.                             {
  53.                                 targetCount = count;
  54.                                 maxBot = Math.Abs(x) + Math.Abs(y) + Math.Abs(z);
  55.                                 bestBot = (x, y, z);
  56.                             }
  57.                             else if (count == targetCount)
  58.                             {
  59.                                 if(maxBot == 0 || Math.Abs(x) + Math.Abs(y) + Math.Abs(z) < maxBot)
  60.                                 {
  61.                                     maxBot = Math.Abs(x) + Math.Abs(y) + Math.Abs(z);
  62.                                     bestBot = (x, y, z);
  63.                                 }
  64.                             }
  65.                         }
  66.                     }
  67.                 }
  68.                 if(distance == 1)
  69.                 {
  70.                     Console.WriteLine($"The max count I have found so far: {targetCount}");
  71.                     return maxBot;
  72.                 }
  73.                 else
  74.                 {
  75.                     xs.Clear();
  76.                     ys.Clear();
  77.                     zs.Clear();
  78.                     xs.Add(bestBot.x - distance);
  79.                     xs.Add(bestBot.x + distance);
  80.                     ys.Add(bestBot.y - distance);
  81.                     ys.Add(bestBot.y + distance);
  82.                     zs.Add(bestBot.z - distance);
  83.                     zs.Add(bestBot.z + distance);
  84.                     distance /= 2;
  85.                 }
  86.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement