Beispiel #1
0
        // theoretisch gezien werkt het....
        public static long Part2Solution(JupiterMoonSystem jupiterMoonSystem, JupiterMoonSystem initialSystem)
        {
            long steps = 0;
            var  historyDoesntRepeat      = true;
            var  moonEqualityComparer     = new MoonEqualityComparer();
            var  velocityEqualityComparer = new VelocityEqualityComparer();

            while (historyDoesntRepeat)
            {
                steps++;
                foreach (var moon in jupiterMoonSystem.GetMoons())
                {
                    var otherMoons = jupiterMoonSystem.Moons.Where(m => m != moon); //hier kun je ook excepten als sneller;
                    foreach (var otherMoon in otherMoons)
                    {
                        moon.V.vX += (otherMoon.X - moon.X) == 0 ? (otherMoon.X - moon.X) : (otherMoon.X - moon.X) / Math.Abs(otherMoon.X - moon.X);
                        moon.V.vY += (otherMoon.Y - moon.Y) == 0 ? (otherMoon.Y - moon.Y) : (otherMoon.Y - moon.Y) / Math.Abs(otherMoon.Y - moon.Y);
                        moon.V.vZ += (otherMoon.Z - moon.Z) == 0 ? (otherMoon.Z - moon.Z) : (otherMoon.Z - moon.Z) / Math.Abs(otherMoon.Z - moon.Z);
                    }
                }

                foreach (var moon in jupiterMoonSystem.GetMoons())
                {
                    moon.Tick();
                }

                if (!initialSystem.Moons.Except(jupiterMoonSystem.Moons, moonEqualityComparer).Any() &&
                    !initialSystem.MoonVelocities.Except(jupiterMoonSystem.MoonVelocities, velocityEqualityComparer).Any())
                {
                    historyDoesntRepeat = false;
                }
            }

            return(steps);
        }
Beispiel #2
0
        public static int Part1Solution(JupiterMoonSystem jupiterMoonSystem, int steps)
        {
            for (int t = 0; t < steps; t++)
            {
                foreach (var moon in jupiterMoonSystem.GetMoons())
                {
                    var otherMoons = jupiterMoonSystem.Moons.Where(m => m != moon);
                    foreach (var otherMoon in otherMoons)
                    {
                        moon.V.vX += (otherMoon.X - moon.X) == 0 ? (otherMoon.X - moon.X) : (otherMoon.X - moon.X) / Math.Abs(otherMoon.X - moon.X);
                        moon.V.vY += (otherMoon.Y - moon.Y) == 0 ? (otherMoon.Y - moon.Y) : (otherMoon.Y - moon.Y) / Math.Abs(otherMoon.Y - moon.Y);
                        moon.V.vZ += (otherMoon.Z - moon.Z) == 0 ? (otherMoon.Z - moon.Z) : (otherMoon.Z - moon.Z) / Math.Abs(otherMoon.Z - moon.Z);
                    }
                }

                foreach (var moon in jupiterMoonSystem.GetMoons())
                {
                    moon.Tick();
                }
            }

            return(jupiterMoonSystem.getTotalEnergy());
        }