private static long Distance(Nanobot n1, Nanobot n2) { return(Math.Abs(n1.X - n2.X) + Math.Abs(n1.Y - n2.Y) + Math.Abs(n1.Z - n2.Z)); }
static void Main(string[] args) { Console.WriteLine("Day 23"); Console.WriteLine("Star 1"); Console.WriteLine(""); nanobots = new List <Nanobot>(); Nanobot biggestRadius = null; foreach (string line in System.IO.File.ReadAllLines(inputFile)) { Nanobot newNanoBot = new Nanobot(line); nanobots.Add(newNanoBot); if (biggestRadius == null || newNanoBot.r > biggestRadius.r) { biggestRadius = newNanoBot; } } int interiorCount = 0; foreach (Nanobot bot in nanobots) { if (biggestRadius.IsWithinRange(bot)) { interiorCount++; } } Console.WriteLine($"There are {interiorCount} bots inside radius."); Console.WriteLine(""); Console.WriteLine("Star 2"); Console.WriteLine(""); //Find the coordinate in range of most //Let's try a binary search //Set up bounds in the laziest way Vector min = new Vector( x: nanobots.Min(x => x.position.x), y: nanobots.Min(x => x.position.y), z: nanobots.Min(x => x.position.z)); Vector max = new Vector( x: nanobots.Max(x => x.position.x) + 1, y: nanobots.Max(x => x.position.y) + 1, z: nanobots.Max(x => x.position.z) + 1); List <Box> bestBoxes = new List <Box>(); List <Box> testBoxes = new List <Box> { new Box(min, max) }; while (true) { //Console.WriteLine("###################################"); //Console.WriteLine(""); //Console.WriteLine("Staring Iteration"); //Console.WriteLine(""); int bestBoxCount = -1; bestBoxes.Clear(); foreach (Box testBox in testBoxes) { foreach (Box box in testBox.SliceBox()) { int count = CountBots(box); //Console.WriteLine($"Box {box}, Count {count}"); if (count > bestBoxCount) { bestBoxes.Clear(); bestBoxes.Add(box); bestBoxCount = count; } else if (count == bestBoxCount) { bestBoxes.Add(box); } } } //Console.WriteLine(""); //Console.WriteLine($"Best Box Bot Count: {bestBoxCount}"); //foreach (Box bestBox in bestBoxes) //{ // Console.WriteLine($" {bestBox}"); //} //Console.WriteLine(""); testBoxes.Clear(); testBoxes.AddRange(bestBoxes); Box testingBox = testBoxes[0]; if ((testingBox.min.x + 1 == testingBox.max.x) && (testingBox.min.y + 1 == testingBox.max.y) && (testingBox.min.z + 1 == testingBox.max.z)) { break; } } Console.WriteLine($"Found {testBoxes.Count} final boxes"); min = testBoxes.Select(x => x.min).OrderBy(x => x.DistanceFromOrigin).First(); Console.WriteLine($"Best Position: {min}, Bots: {CountBots(min)}, Distance from Origin: {min.DistanceFromOrigin}"); Console.WriteLine(""); Console.ReadKey(); }