Example #1
0
        protected override long Part2(string[] input)
        {
            var moons = input
                        .Select(x => Moon.ParseFrom(x))
                        .ToArray();
            var planet = new Planet(moons);
            var xvec   = planet.XVectors.ToArray();
            var yvec   = planet.YVectors.ToArray();
            var zvec   = planet.ZVectors.ToArray();
            var xcycle = 0L;
            var ycycle = 0L;
            var zcycle = 0L;

            while (xcycle == 0 || ycycle == 0 || zcycle == 0)
            {
                planet.SimulateMotionStep();
                if (xcycle == 0 && planet.XVectors.SequenceEqual(xvec))
                {
                    xcycle = planet.SimulationStep;
                }
                if (ycycle == 0 && planet.YVectors.SequenceEqual(yvec))
                {
                    ycycle = planet.SimulationStep;
                }
                if (zcycle == 0 && planet.ZVectors.SequenceEqual(zvec))
                {
                    zcycle = planet.SimulationStep;
                }
            }

            var cycle = MathHelper.LeastCommonMultiple(xcycle, ycycle, zcycle);

            return(cycle);
        }
Example #2
0
        protected override long Part1(string[] input)
        {
            var moons = input
                        .Select(x => Moon.ParseFrom(x))
                        .ToArray();
            var jupiter = new Planet(moons);

            for (var i = 0; i < 1000; i++)
            {
                jupiter.SimulateMotionStep();
            }
            var totalEnergy = jupiter.TotalEnergy;

            return(totalEnergy);
        }
Example #3
0
 public void ApplyGravityFrom(Moon otherMoon)
 {
     Vx += X == otherMoon.X ? 0 : X < otherMoon.X ? 1 : -1;
     Vy += Y == otherMoon.Y ? 0 : Y < otherMoon.Y ? 1 : -1;
     Vz += Z == otherMoon.Z ? 0 : Z < otherMoon.Z ? 1 : -1;
 }