public void AddNeighborIfInRange(Nanobot b) { if (Distance(this, b) <= Range) { Neighbors.Add(b); } }
public void stopDragging() { Destroy(mouseFollowingSprite); mouseFollowingSprite = null; nanobotPrefab = null; CellHighlighter.clearHighlights(); }
private void ReadPositions() { var lines = _stream.ReadStringDocument(); const string pattern = @"pos=<([\d-]+),([\d-]+),([\d-]+)>, r=(\d+)"; foreach (var line in lines) { var groups = Regex.Match(line, pattern).Groups; var nanobot = new Nanobot { Position = new int[3] { int.Parse(groups[1].Value), int.Parse(groups[2].Value), int.Parse(groups[3].Value), }, Radius = int.Parse(groups[4].Value) }; _nanobots.Add(nanobot); if (_selected.Radius < nanobot.Radius) { _selected = nanobot; } } }
internal override void VerifyPartners(Assignments assignments, Nanobot thisBot) { Region r = this.GetRegion(thisBot); int n = 1; foreach ((Nanobot thatBot, Command cmd) in assignments) { if (thisBot == thatBot) { continue; } if (cmd is GFillCommand that && that.GetRegion(thatBot).Equals(r)) { Coord thisPos = thisBot.Pos + this.mNd; Coord thatPos = thatBot.Pos + that.mNd; Verify(thisPos != thatPos, $"{thisBot} and {thatBot} are both pointing to {thisPos}."); ++n; } } Verify(n == (1 << r.Dim()), "Incomplete GFill."); }
internal override void VerifyPreconds(State state, Nanobot botS) { Coord posS = botS.Pos; Coord posP = posS + mNd; Verify(state.Matrix.Contains(posP), $"{posP} is out of the matrix."); }
public static void PartOne() { List <string> lines = Utils.GetLinesFromFile("input/Day23Input.txt"); IEnumerable <Nanobot> nanobots = lines.Select(x => { string firstPart = x.Substring(5); string[] splitOne = firstPart.Split(">"); string[] numbers = splitOne[0].Split(","); string radiusString = splitOne[1].Split("=")[1]; return(new Nanobot(Convert.ToInt32(numbers[0]), Convert.ToInt32(numbers[1]), Convert.ToInt32(numbers[2]), Convert.ToInt32(radiusString))); }); int maxRadius = nanobots.Select(x => x.radius).Max(); Nanobot maxBot = nanobots.Where(x => x.radius == maxRadius).ElementAt(0); int botsInRange = nanobots.Where(x => { int xDiff = Math.Abs(x.x - maxBot.x); int yDiff = Math.Abs(x.y - maxBot.y); int zDiff = Math.Abs(x.z - maxBot.z); return((xDiff + yDiff + zDiff) <= maxBot.radius); }).Count(); Console.WriteLine(botsInRange); }
public override object Task1() { bots = new Nanobot[input.Length]; for (int i = 0; i < bots.Length; i++) { bots[i] = new Nanobot(input[i]); } Nanobot strongest = bots[0]; for (int i = 1; i < bots.Length; i++) { if (bots[i].Radius > strongest.Radius) { strongest = bots[i]; } } int count = 0; foreach (Nanobot n in bots) { if (ManhattanDistance(n, strongest) <= strongest.Radius) { count++; } } return(count); }
public GameObject moveBot(GridPosition source, Nanobot nanobot, GridPosition offset) { GridPosition newPosition = applyDelta(source, offset); GameObject newBot = placeBot(newPosition, nanobot); DestroyNanobotAt(source); return(newBot); }
private bool Intersect(Nanobot nanobotFrom, Nanobot nanobotTo) { var distance = Math.Sqrt((nanobotFrom.X - nanobotTo.X) * (nanobotFrom.X - nanobotTo.X) + (nanobotFrom.Y - nanobotTo.Y) * (nanobotFrom.Y - nanobotTo.Y) + (nanobotFrom.Z - nanobotTo.Z) * (nanobotFrom.Z - nanobotTo.Z)); return(distance < (nanobotFrom.Radius + nanobotFrom.Radius)); }
internal override void VerifyPartners(Assignments assignments, Nanobot botS) { (Nanobot botP, Command cmdP) = assignments.FirstOrDefault( bot_cmd => bot_cmd.Item1.Pos == botS.Pos + mNd); Verify(botP != null, $"No bot exists at {botS.Pos + mNd}."); Verify(cmdP is FusionPCommand, $"{botP} is not performing FusionP."); }
public void CreationTests(string input, int x, int y, int z, int radius) { var sut = new Nanobot(input); Assert.AreEqual(x, sut.Position.X); Assert.AreEqual(y, sut.Position.Y); Assert.AreEqual(z, sut.Position.Z); Assert.AreEqual(radius, sut.Radius); }
internal override void VerifyPreconds(State state, Nanobot bot) { Coord c0 = bot.Pos; Coord c1 = c0 + mNd; Coord c2 = c1 + mFd; Verify(state.Matrix.Contains(c1), $"{c1} is out of the matrix."); Verify(state.Matrix.Contains(c2), $"{c2} is out of the matrix."); }
internal override void ApplyToState(State state, Nanobot botP) { Nanobot botS = state.Bots.First(bot => bot.Pos == botP.Pos + mNd); state.RemoveBot(botS); botP.Fusion(botS); state.Energy -= 24; }
static void Main(string[] args) { List <Nanobot> bots = new List <Nanobot>(); foreach (string line in File.ReadAllLines("input.txt")) { int rstart = line.LastIndexOf('=') + 1; int openBracket = line.IndexOf('<'); string[] coords = line.Substring(openBracket + 1, line.IndexOf('>') - openBracket - 1).Split(','); Nanobot bot = new Nanobot() { R = int.Parse(line.Substring(rstart)), X = int.Parse(coords[0]), Y = int.Parse(coords[1]), Z = int.Parse(coords[2]) }; bots.Add(bot); } int maxR = bots.Max(b => b.R); Nanobot maxRBot = bots.First(bot => bot.R == maxR); int botsInRange = bots.Where(b => ManhattanDistance(b, maxRBot) <= maxR).Count(); Console.WriteLine(botsInRange); // Part 2, inspired by https://www.reddit.com/r/adventofcode/comments/a8s17l/2018_day_23_solutions/ecdqzdg/ SortedDictionary <int, int> ranges = new SortedDictionary <int, int>(); foreach (var bot in bots) { var dist = ManhattanDistance(bot, 0, 0, 0); ranges[Math.Max(0, dist - bot.R)] = 1; ranges[dist + bot.R] = -1; } int botsInRangePart2 = 0; int maxBotsInRange = 0; int distancePart2 = 0; foreach (var range in ranges) { botsInRangePart2 += range.Value; if (botsInRangePart2 > maxBotsInRange) { maxBotsInRange = botsInRange; distancePart2 = range.Key; } } Console.WriteLine(distancePart2); Console.ReadLine(); }
public string Run(Aoc.Framework.Part part) { if (part == Aoc.Framework.Part.Part1) { // Get the strongest nanobot Nanobot strongest = _nanobots.OrderByDescending(n => n.Radius).First(); // Count the nanobot in range return(_nanobots.Count(n => Nanobot.Distance(n, strongest) <= strongest.Radius).ToString()); } if (part == Aoc.Framework.Part.Part2) { // Find the coordinates with the most nanobots in range int minSize = _nanobots.Min(n => Math.Min(Math.Min(n.X, n.Y), n.Z) - n.Radius); int maxSize = _nanobots.Min(n => Math.Max(Math.Max(n.X, n.Y), n.Z) + n.Radius); int maxBound = Math.Max(Math.Abs(minSize), Math.Abs(maxSize)); int boxSize = 1; while (boxSize < maxBound) { boxSize *= 2; } // Use a priority queue to process cubes in order // Priority is given by : // 1. Number of bots in range // 2. Distance to the origin // Hence the priority weight given by ((long)nbBot << 32) + (long)dst) PriorityQueue <Box> queue = new PriorityQueue <Box>(); queue.Enqueue(new Box(boxSize), 0); while (queue.TryDequeueMax(out var box)) { // If it's the minimal size if (box.X2 == box.X1) { // This is the closest return(box.DistanceToOrigin().ToString()); } // Split the box in 8 boxes Box[] split = box.Split(); foreach (Box b in split) { int nbBot = _nanobots.Count(n => n.IntersectWithBox(b)); int dst = box.DistanceToOrigin(); queue.Enqueue(b, ((long)nbBot << 32) + (long)dst); } } } return(""); }
public GameObject placeBot(GridPosition position, Nanobot nanobot) { if (nanobot.schematic == null || position == null) { return(null); } GameObject newBot = Instantiate(nanobot.gameObject); GridPositionComponent gridPosition = newBot.GetComponent <GridPositionComponent>(); setBotPosition(newBot, gridPosition, position); return(newBot); }
internal override void ApplyToState(State state, Nanobot bot) { if (!IsOrigin) { return; } Region r = Region.Of(bot.Pos + mNd, bot.Pos + mNd + mFd); foreach (Coord c in r.GetMembers()) { state.FillVoxel(c); } }
protected override bool Attack() { Retarget(TowerProperties.Range); if (!TargetSet) { return(false); } CurrentCooldown = TowerProperties.Cooldown; Nanobot shot = Instantiate(shotPrefab, transform.position, Quaternion.identity).GetComponent <Nanobot>(); shot.setProperties(ShotProperties as NanoBotProperties); shot.SetTarget(Target); return(true); }
internal override IEnumerable <Coord> GetVolatile(Nanobot bot) { yield return(bot.Pos); if (!IsOrigin) { yield break; } Region r = Region.Of(bot.Pos + mNd, bot.Pos + mNd + mFd); foreach (Coord c in r.GetMembers()) { yield return(c); } }
public bool InRange(Nanobot other) { var distance = 0; for (int i = 0; i < Position.Length; i++) { var value1 = Position[i]; var value2 = other.Position[i]; if ((value1 > 0 && value2 > 0) || (value1 < 0 && value2 < 0)) { distance += Math.Abs(Math.Abs(value1) - Math.Abs(value2)); } else { distance += Math.Abs(value1) + Math.Abs(value2); } } return(distance <= Radius); }
public void clickNanobot(Nanobot nanobotPrefab) { if (SoundManager.instance != null) { SoundManager.instance.PlaySingle(GetComponent <AudioSource>(), selectBotSound); } if (mouseFollowingSprite != null) { Destroy(mouseFollowingSprite); } this.nanobotPrefab = nanobotPrefab; mouseFollowingSprite = new GameObject(); mouseFollowingSprite.AddComponent <SpriteRenderer>().sprite = nanobotPrefab.GetComponent <SpriteRenderer>().sprite; mouseFollowingSprite.GetComponent <SpriteRenderer>().sortingOrder = 3; mouseFollowingSprite.transform.localScale = new Vector3(0.5f, 0.5f, 1); Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition); mouseFollowingSprite.transform.position = new Vector3(position.x, position.y, mouseFollowingSprite.transform.position.z); CellHighlighter.triggerHighlights(); }
public bool TryAdd(Nanobot s) { if (_members.Contains(s)) { return(true); } if (_unmembers.Contains(s)) { return(false); } foreach (var m in _members) { if (!(m.Intersects(s))) { _unmembers.Add(s); return(false); } } _members.Add(s); return(true); }
internal override void ApplyToState(State state, Nanobot botS) { // Everything is done by FusionP. }
internal override IEnumerable <Coord> GetVolatile(Nanobot botS) { yield return(botS.Pos); // (botS.Pos + mNd) is cared by FusionP. }
internal abstract void ApplyToState(State state, Nanobot bot);
internal abstract IEnumerable <Coord> GetVolatile(Nanobot bot);
internal virtual void VerifyPartners( Assignments assignments, Nanobot bot) { }
internal virtual void VerifyPreconds(State state, Nanobot bot) { }
public static long Distance(Nanobot a, Nanobot b) { return(Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y) + Math.Abs(a.Z - b.Z)); }
private static int CompareByRange(Nanobot x, Nanobot y) { return(y.Range.CompareTo(x.Range)); }