Example #1
0
        public override string Part2(string input)
        {
            var planets = RegexDeserializable.Deserialize <Point3>(input).ToArray();

            var x = Cycle(new[] { planets[0].x, planets[1].x, planets[2].x, planets[3].x });
            var y = Cycle(new[] { planets[0].y, planets[1].y, planets[2].y, planets[3].y });
            var z = Cycle(new[] { planets[0].z, planets[1].z, planets[2].z, planets[3].z });

            Console.WriteLine($"{x} {y} {z}");

            return(Utils.lcm(Utils.lcm(x, y), z).ToString());
        }
Example #2
0
        int simulate(string input, int iterations)
        {
            var planets    = RegexDeserializable.Deserialize <Point3>(input).ToArray();
            var velocities = new Point3[planets.Length];

            foreach (var i in Enumerable.Range(0, iterations))
            {
                for (int i1 = 0; i1 < planets.Length; i1++)
                {
                    for (int i2 = i1 + 1; i2 < planets.Length; i2++)
                    {
                        var p1 = planets[i1];
                        var p2 = planets[i2];

                        var adjustment = new Point3(Gravity(p1.x, p2.x), Gravity(p1.y, p2.y), Gravity(p1.z, p2.z));

                        velocities[i1] = velocities[i1].Offset(adjustment);
                        velocities[i2] = velocities[i2].Offset(adjustment.Scale(-1));
                    }
                }

                for (int i1 = 0; i1 < planets.Length; i1++)
                {
                    planets[i1] = planets[i1].Offset(velocities[i1]);
                }

                /*
                 * Console.WriteLine(i + ":");
                 * for (int i1 = 0; i1 < planets.Length; i1++)
                 * {
                 *  Console.WriteLine(planets[i1] + " " + velocities[i1]);
                 * }
                 */
            }

            return(planets.Zip(velocities.AsEnumerable(), (p, v) => p.Magnitude * v.Magnitude).Sum());
        }