static void Main(string[] args)
        {
            //var originalCode = File.ReadAllText("input.txt").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
            //var extendedCode = new long[1000000];
            //Array.Copy(originalCode, extendedCode, originalCode.Length);

            //IntCode computer = new IntCode(extendedCode);
            //computer.Process(2);
            //Console.WriteLine(computer.GetOutput());

            //var originalCode = File.ReadAllText("input.txt").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
            //var extendedCode = new long[1000000];
            //Array.Copy(originalCode, extendedCode, originalCode.Length);
            //var robot = new Robot(extendedCode);
            //Console.WriteLine(robot.Solve1());

            Moon[] moons = new Moon[4];
            int    i     = 0;

            foreach (string line in File.ReadLines("input.txt"))
            {
                var moon = new Moon();
                moon.Parse(line);
                moons[i++] = moon;
            }

            Console.WriteLine(Day12.Solve2(moons));
        }
Exemple #2
0
 public void ApplyAllGravity(Moon moon)
 {
     if (x < moon.x)
     {
         vx++;
     }
     else if (x > moon.x)
     {
         vx--;
     }
     if (y < moon.y)
     {
         vy++;
     }
     else if (y > moon.y)
     {
         vy--;
     }
     if (z < moon.z)
     {
         vz++;
     }
     else if (z > moon.z)
     {
         vz--;
     }
 }
Exemple #3
0
 public void Gravitate(Moon other)
 {
     // If other == this, we automatically skip, because positions are equal
     for (int i = 0; i < 3; i++)
     {
         if (this.pos[i] < other.pos[i])
         {
             v[i]++;
         }
         if (this.pos[i] > other.pos[i])
         {
             v[i]--;
         }
     }
 }
Exemple #4
0
 private static string ConcatAxisState(string init, Moon m, int axis)
 {
     if (axis == 0)
     {
         init += m.x + " ";
         init += m.vx + " ";
         return(init);
     }
     if (axis == 1)
     {
         init += m.y + " ";
         init += m.vy + " ";
         return(init);
     }
     init += m.z + " ";
     init += m.vz + " ";
     return(init);
 }
Exemple #5
0
 public void ApplyGravity(Moon moon, int i)
 {
     if (i == 0)
     {
         if (x < moon.x)
         {
             vx++;
         }
         else if (x > moon.x)
         {
             vx--;
         }
     }
     if (i == 1)
     {
         if (y < moon.y)
         {
             vy++;
         }
         else if (y > moon.y)
         {
             vy--;
         }
     }
     if (i == 2)
     {
         if (z < moon.z)
         {
             vz++;
         }
         else if (z > moon.z)
         {
             vz--;
         }
     }
 }
        public static long Solve2(Moon[] moons)
        {
            Moon[] initialMoons = new Moon[4];
            for (int i = 0; i < 4; i++)
            {
                initialMoons[i] = new Moon
                {
                    PosX = moons[i].PosX,
                    PosY = moons[i].PosY,
                    PosZ = moons[i].PosZ
                };
            }
            var positionFoundX = 0;
            var positionFoundY = 0;
            var positionFoundZ = 0;

            for (int i = 1; ; i++)
            {
                ApplyGravity(moons);
                UpdatePosition(moons);

                int foundOk = 0;
                for (int j = 0; j < 4; j++)
                {
                    if (moons[j].PosX == initialMoons[j].PosX && moons[j].VelocityX == 0)
                    {
                        foundOk++;
                    }
                }

                if (foundOk == 4)
                {
                    positionFoundX = i;
                    break;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                moons[i] = new Moon
                {
                    PosX = initialMoons[i].PosX,
                    PosY = initialMoons[i].PosY,
                    PosZ = initialMoons[i].PosZ
                };
            }

            for (int i = 1;; i++)
            {
                ApplyGravity(moons);
                UpdatePosition(moons);

                int foundOk = 0;
                for (int j = 0; j < 4; j++)
                {
                    if (moons[j].PosY == initialMoons[j].PosY && moons[j].VelocityY == 0)
                    {
                        foundOk++;
                    }
                }

                if (foundOk == 4)
                {
                    positionFoundY = i;
                    break;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                moons[i] = new Moon
                {
                    PosX = initialMoons[i].PosX,
                    PosY = initialMoons[i].PosY,
                    PosZ = initialMoons[i].PosZ
                };
            }

            for (int i = 1; ; i++)
            {
                ApplyGravity(moons);
                UpdatePosition(moons);

                int foundOk = 0;
                for (int j = 0; j < 4; j++)
                {
                    if (moons[j].PosZ == initialMoons[j].PosZ && moons[j].VelocityZ == 0)
                    {
                        foundOk++;
                    }
                }

                if (foundOk == 4)
                {
                    positionFoundZ = i;
                    break;
                }
            }

            var lcm = determineLCM(positionFoundX, positionFoundY);

            return(determineLCM(lcm, positionFoundZ));
        }
Exemple #7
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Day 12");

            // Part 1
            Moon[] moon = new Moon[] { new Moon(15, -2, -6),
                                       new Moon(-5, -4, -11),
                                       new Moon(0, -6, 0),
                                       new Moon(5, 9, 6) };

            for (int t = 0; t < 1000; t++)
            {
                for (int m = 0; m < moon.Length; m++)
                {
                    for (int n = 0; n < moon.Length; n++)
                    {
                        moon[m].Gravitate(moon[n]);
                    }
                }
                foreach (Moon m in moon)
                {
                    m.Move();
                }
            }
            int E = 0;

            foreach (Moon m in moon)
            {
                E += m.GetTotalEnergy();
            }

            System.Console.WriteLine($"1. {E}");

            // Part 2
            // int[] X = new int[] { -1, 2, 4, 3 };
            // int[] Y = new int[] { 0, -10, -8, 5 };
            // int[] Z = new int[] { 2, -7, 8, -1 };
            int[] X = new int[] { 15, -5, 0, 5 };
            int[] Y = new int[] { -2, -4, -6, 9 };
            int[] Z = new int[] { -6, -11, 0, 6 };

            long[] period = new long[3];
            period[0] = GetPeriod(X);
            System.Console.WriteLine($"{period[0]}");
            period[1] = GetPeriod(Y);
            System.Console.WriteLine($"{period[1]}");
            period[2] = GetPeriod(Z);
            System.Console.WriteLine($"{period[2]}");

            long lcm1 = lcm(period[0], period[1]);
            long lcm2 = lcm(lcm1, period[2]);

            System.Console.WriteLine($"2. {lcm2}");

            long GetPeriod(int[] pos)
            {
                int[] u = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    u[i] = pos[i];
                }

                int[] v = new int[] { 0, 0, 0, 0 };
                for (long t = 0; t < Int64.MaxValue; t++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            if (u[i] < u[j])
                            {
                                v[i]++;
                            }
                            if (u[i] > u[j])
                            {
                                v[i]--;
                            }
                        }
                    }
                    for (int i = 0; i < 4; i++)
                    {
                        u[i] += v[i];
                    }
                    bool match = true;
                    for (int i = 0; i < 4; i++)
                    {
                        if (u[i] != pos[i] || v[i] != 0)
                        {
                            match = false;
                        }
                    }
                    if (match)
                    {
                        return(t + 1);
                    }
                }
                return(-1);
            }
        }