コード例 #1
0
ファイル: Day23.cs プロジェクト: tslater2006/AdventOfCode
        /* Algorithm/explanation: https://www.reddit.com/r/adventofcode/comments/a8sqov/help_day_23_part_2_any_provably_correct_fast/ecfag7f */
        internal override string SolvePart2()
        {
            PriorityQueue <NanoBot> q = new PriorityQueue <NanoBot>();
            /* create a bot that has every other bot in range */
            var maxRadius = Bots.Select(b => Math.Abs(b.X) + Math.Abs(b.Y) + Math.Abs(b.Z)).Max();
            var searchBot = new NanoBot()
            {
                X = 0, Y = 0, Z = 0, R = maxRadius
            };

            var intersectCount = Bots.Where(b => b.Intersects(searchBot)).Count();

            q.AddOrUpdate(searchBot, intersectCount);

            while (q.Count > 0)
            {
                var maxPriority = q._priorities.Max();
                searchBot = q._dataByPriority[maxPriority].OrderBy(d => d.DistanceTo(0, 0, 0)).OrderBy(d => d.R).First();

                q.DequeueItem(searchBot);

                if (searchBot.R <= 0)
                {
                    return(searchBot.DistanceTo(0, 0, 0).ToString());
                }
                else
                {
                    foreach (var bot in searchBot.SplitBot())
                    {
                        intersectCount = Bots.Where(b => b.Intersects(bot)).Count();

                        q.AddOrUpdate(bot, intersectCount);
                    }
                }
            }

            return("??");
        }