Example #1
0
 public Moon(Moon m)
 {
     x  = m.x;
     y  = m.y;
     z  = m.z;
     dx = dy = dz = 0;
 }
Example #2
0
        public void ApplyGravity(Moon m)
        {
            if (x < m.x)
            {
                dx   += 1;
                m.dx -= 1;
            }
            else if (x > m.x)
            {
                dx   -= 1;
                m.dx += 1;
            }

            if (y < m.y)
            {
                dy   += 1;
                m.dy -= 1;
            }
            else if (y > m.y)
            {
                dy   -= 1;
                m.dy += 1;
            }

            if (z < m.z)
            {
                dz   += 1;
                m.dz -= 1;
            }
            else if (z > m.z)
            {
                dz   -= 1;
                m.dz += 1;
            }
        }
Example #3
0
        private void ApplyGravity(Moon moon1, Moon moon2)
        {
            var xDiff = moon1.Position.X - moon2.Position.X;

            if (xDiff != 0)
            {
                xDiff /= Math.Abs(xDiff);
            }
            var yDiff = moon1.Position.Y - moon2.Position.Y;

            if (yDiff != 0)
            {
                yDiff /= Math.Abs(yDiff);
            }
            var zDiff = moon1.Position.Z - moon2.Position.Z;

            if (zDiff != 0)
            {
                zDiff /= Math.Abs(zDiff);
            }

            moon1.Velocity.X -= xDiff;
            moon2.Velocity.X += xDiff;

            moon1.Velocity.Y -= yDiff;
            moon2.Velocity.Y += yDiff;

            moon1.Velocity.Z -= zDiff;
            moon2.Velocity.Z += zDiff;
        }
Example #4
0
        public void AdjustVelocityFromGravityOf(Moon m)
        {
            if (m.X > this.X)
            {
                XVelocity++;
            }
            else if (m.X < this.X)
            {
                XVelocity--;
            }

            if (m.Y > this.Y)
            {
                YVelocity++;
            }
            else if (m.Y < this.Y)
            {
                YVelocity--;
            }

            if (m.Z > this.Z)
            {
                ZVelocity++;
            }
            else if (m.Z < this.Z)
            {
                ZVelocity--;
            }
        }
Example #5
0
 public static void AdjustVelocities(Moon moon1, Moon moon2)
 {
     moon1.Velocity.X += moon1.Position.X < moon2.Position.X ? 1 : moon1.Position.X == moon2.Position.X ? 0 : -1;
     moon2.Velocity.X += moon1.Position.X > moon2.Position.X ? 1 : moon1.Position.X == moon2.Position.X ? 0 : -1;
     moon1.Velocity.Y += moon1.Position.Y < moon2.Position.Y ? 1 : moon1.Position.Y == moon2.Position.Y ? 0 : -1;
     moon2.Velocity.Y += moon1.Position.Y > moon2.Position.Y ? 1 : moon1.Position.Y == moon2.Position.Y ? 0 : -1;
     moon1.Velocity.Z += moon1.Position.Z < moon2.Position.Z ? 1 : moon1.Position.Z == moon2.Position.Z ? 0 : -1;
     moon2.Velocity.Z += moon1.Position.Z > moon2.Position.Z ? 1 : moon1.Position.Z == moon2.Position.Z ? 0 : -1;
 }
Example #6
0
        private void ApplyGravity()
        {
            var pairs = Moons.SelectMany((first, i) => Moons.Skip(i + 1).Select(second => (first, second)));

            foreach (var pair in pairs)
            {
                Moon.AdjustVelocities(pair.first, pair.second);
            }
        }
Example #7
0
        public Moon(Moon m)
        {
            this.X = m.X;
            this.Y = m.Y;
            this.Z = m.Z;

            this.XVelocity = m.XVelocity;
            this.YVelocity = m.YVelocity;
            this.ZVelocity = m.ZVelocity;
        }
Example #8
0
 void SimulateZCoordenates(Moon m, Moon otherMoon)
 {
     if (m.z > otherMoon.z)
     {
         m.vz--;
         otherMoon.vz++;
     }
     else if (m.z < otherMoon.z)
     {
         m.vz++;
         otherMoon.vz--;
     }
 }
Example #9
0
 void SimulateYCoordenates(Moon m, Moon otherMoon)
 {
     if (m.y > otherMoon.y)
     {
         m.vy--;
         otherMoon.vy++;
     }
     else if (m.y < otherMoon.y)
     {
         m.vy++;
         otherMoon.vy--;
     }
 }
Example #10
0
 void SimulateXCoordenates(Moon m, Moon otherMoon)
 {
     if (m.x > otherMoon.x)
     {
         m.vx--;
         otherMoon.vx++;
     }
     else if (m.x < otherMoon.x)
     {
         m.vx++;
         otherMoon.vx--;
     }
 }
Example #11
0
        // To apply gravity, consider every pair of moons.On each axis(x, y, and z), the velocity of each moon changes
        // by exactly +1 or -1 to pull the moons together.For example, if Ganymede has an x position of 3, and Callisto
        // has a x position of 5, then Ganymede's x velocity changes by +1 (because 5 > 3) and Callisto's x velocity
        // changes by -1 (because 3 < 5). However, if the positions on a given axis are the same, the velocity on that
        // axis does not change for that pair of moons.
        public static void ApplyGravity(List <Moon> moons)
        {
            for (int i = 0; i < (moons.Count - 1); i++)
            {
                for (int j = i + 1; j < moons.Count; j++)
                {
                    Moon currentMoon = moons[i];
                    Moon otherMoon   = moons[j];

                    int positionXDiff = currentMoon.positionX - otherMoon.positionX;
                    if (positionXDiff > 0)
                    {
                        currentMoon.velocityX--;
                        otherMoon.velocityX++;
                    }
                    else if (positionXDiff < 0)
                    {
                        currentMoon.velocityX++;
                        otherMoon.velocityX--;
                    }

                    int positionYDiff = currentMoon.positionY - otherMoon.positionY;
                    if (positionYDiff > 0)
                    {
                        currentMoon.velocityY--;
                        otherMoon.velocityY++;
                    }
                    else if (positionYDiff < 0)
                    {
                        currentMoon.velocityY++;
                        otherMoon.velocityY--;
                    }

                    int positionZDiff = currentMoon.positionZ - otherMoon.positionZ;
                    if (positionZDiff > 0)
                    {
                        currentMoon.velocityZ--;
                        otherMoon.velocityZ++;
                    }
                    else if (positionZDiff < 0)
                    {
                        currentMoon.velocityZ++;
                        otherMoon.velocityZ--;
                    }
                }
            }
        }
Example #12
0
        private static List <Moon> GetMoons(string input)
        {
            List <Moon> moons = new List <Moon>();

            string[] lines = input.Split(Environment.NewLine);
            //<x=-5, y=-4, z=-4>
            Regex regex = new Regex(@"<x=(?<x>-?\d+), y=(?<y>-?\d+), z=(?<z>-?\d+)");

            foreach (string line in lines)
            {
                Match m = regex.Match(line);
                if (m.Success)
                {
                    Moon moon = new Moon(int.Parse(m.Groups["x"].Value),
                                         int.Parse(m.Groups["y"].Value),
                                         int.Parse(m.Groups["z"].Value));
                    moons.Add(moon);
                }
            }
            return(moons);
        }
Example #13
0
        public void Part2()
        {
            Moon[] Data = TestData;

            Moon[] InitData = new Moon[Data.Length];
            for (int m = 0; m < Data.Length; m++)
            {
                InitData[m] = new Moon(Data[m]);
            }

            int step = 1;

            Step(Data);
            while (!SamePos(InitData, Data))
            {
                Step(Data);
                step++;
            }

            Console.WriteLine("Day12 Part2 Result = {0}", step);
        }
Example #14
0
 public bool SamePos(Moon m)
 {
     return((m.x == x) && (m.y == y) && (m.z == z) && (m.dx == dx) && (m.dy == y) && (m.dz == z));
 }
Example #15
0
        public static void Problem(int iterations, string[] moonStrings, out int energyAfterIterations, out long returnToOrigin)
        {
            if (moonStrings == null)
            {
                throw new NullReferenceException();
            }
            Moon[] moons         = new Moon[moonStrings.Length];
            Moon[] originalMoons = new Moon[moonStrings.Length];
            for (int i = 0; i < moonStrings.Length; i++)
            {
                moons[i]         = new Moon(moonStrings[i]);
                originalMoons[i] = new Moon(moonStrings[i]);
            }

            /*
             * The x, y, and z values are independent - for part 2, find the repeat periods of
             * each individual part and then find the least common multiple of the 3 values
             */
            long xPeriod = Int64.MaxValue;
            long yPeriod = Int64.MaxValue;
            long zPeriod = Int64.MaxValue;

            //fixed number of iterations for part 1
            for (int i = 1; i <= iterations; i++)
            {
                ApplyGravityAndMove(moons);

                //check if x's, y's, or z's are in initial state - if so, set period for that axis
                if (xPeriod == Int64.MaxValue && CompareX(originalMoons, moons))
                {
                    xPeriod = i;
                }
                if (yPeriod == Int64.MaxValue && CompareY(originalMoons, moons))
                {
                    yPeriod = i;
                }
                if (zPeriod == Int64.MaxValue && CompareZ(originalMoons, moons))
                {
                    zPeriod = i;
                }

                ////debugging use only
                //Console.WriteLine($"after {(i + 1).ToString()} steps:");
                //for (int j = 0; j < 4; j++)
                //{
                //	Console.WriteLine(moons[j].ToString());
                //}
            }

            int energy = 0;

            for (int i = 0; i < 4; i++)
            {
                energy += moons[i].Energy;
            }
            energyAfterIterations = energy;


            //loop until repeat periods for X, Y, and Z found for part 2
            long loops = iterations;

            while (xPeriod == Int64.MaxValue || yPeriod == Int64.MaxValue || zPeriod == Int64.MaxValue)
            {
                ApplyGravityAndMove(moons);
                loops++;
                //check if x's, y's, or z's are in initial state - if so, set period for that axis
                if (xPeriod == Int64.MaxValue && CompareX(originalMoons, moons))
                {
                    xPeriod = loops;
                }
                if (yPeriod == Int64.MaxValue && CompareY(originalMoons, moons))
                {
                    yPeriod = loops;
                }
                if (zPeriod == Int64.MaxValue && CompareZ(originalMoons, moons))
                {
                    zPeriod = loops;
                }
            }

            returnToOrigin = LCM(xPeriod, LCM(yPeriod, zPeriod));

            //debugging:
            //Console.WriteLine($"xPeriod:{xPeriod.ToString()},yPeriod:{yPeriod.ToString()},zPeriod:{zPeriod.ToString()}");
        }
Example #16
0
 public void ApplyGravity(Moon m)
 {
     VX += m.X.CompareTo(X);
     VY += m.Y.CompareTo(Y);
     VZ += m.Z.CompareTo(Z);
 }