Ejemplo n.º 1
0
 public void AddNeighborIfInRange(Nanobot b)
 {
     if (Distance(this, b) <= Range)
     {
         Neighbors.Add(b);
     }
 }
Ejemplo n.º 2
0
 public void stopDragging()
 {
     Destroy(mouseFollowingSprite);
     mouseFollowingSprite = null;
     nanobotPrefab        = null;
     CellHighlighter.clearHighlights();
 }
Ejemplo n.º 3
0
        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;
                }
            }
        }
Ejemplo n.º 4
0
            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.");
            }
Ejemplo n.º 5
0
            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.");
            }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
    public GameObject moveBot(GridPosition source, Nanobot nanobot, GridPosition offset)
    {
        GridPosition newPosition = applyDelta(source, offset);
        GameObject   newBot      = placeBot(newPosition, nanobot);

        DestroyNanobotAt(source);
        return(newBot);
    }
Ejemplo n.º 9
0
        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));
        }
Ejemplo n.º 10
0
 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.");
 }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
            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.");
            }
Ejemplo n.º 13
0
            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;
            }
Ejemplo n.º 14
0
    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();
    }
Ejemplo n.º 15
0
        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("");
        }
Ejemplo n.º 16
0
    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);
    }
Ejemplo n.º 17
0
            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);
                }
            }
Ejemplo n.º 18
0
    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);
    }
Ejemplo n.º 19
0
            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);
                }
            }
Ejemplo n.º 20
0
            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);
            }
Ejemplo n.º 21
0
    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();
    }
Ejemplo n.º 22
0
    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);
    }
Ejemplo n.º 23
0
 internal override void ApplyToState(State state, Nanobot botS)
 {
     // Everything is done by FusionP.
 }
Ejemplo n.º 24
0
 internal override IEnumerable <Coord> GetVolatile(Nanobot botS)
 {
     yield return(botS.Pos);  // (botS.Pos + mNd) is cared by FusionP.
 }
Ejemplo n.º 25
0
 internal abstract void ApplyToState(State state, Nanobot bot);
Ejemplo n.º 26
0
 internal abstract IEnumerable <Coord> GetVolatile(Nanobot bot);
Ejemplo n.º 27
0
 internal virtual void VerifyPartners(
     Assignments assignments, Nanobot bot)
 {
 }
Ejemplo n.º 28
0
 internal virtual void VerifyPreconds(State state, Nanobot bot)
 {
 }
Ejemplo n.º 29
0
 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));
 }
Ejemplo n.º 30
0
 private static int CompareByRange(Nanobot x, Nanobot y)
 {
     return(y.Range.CompareTo(x.Range));
 }