示例#1
0
        public string Part1(string[] input)
        {
            var moons     = ParseMoons(input);
            var simulator = new MoonSimulator(moons);

            simulator.Simulate(1000);
            return(simulator.TotalEnergy.ToString());
        }
示例#2
0
        public string Part2(string[] input)
        {
            var moons     = ParseMoons(input);
            var simulator = new MoonSimulator(moons);
            var ticks     = 0L;
            var xCycles   = 0L;
            var yCycles   = 0L;
            var zCycles   = 0L;

            var moonMappings = simulator.MoonsToOriginalMoons;

            while (true)
            {
                ticks++;

                simulator.Step();

                if (moons.All(m => Moon.XComparer.Equals(m, moonMappings[m]) && xCycles == 0))
                {
                    xCycles = ticks;
                }

                if (moons.All(m => Moon.YComparer.Equals(m, moonMappings[m]) && yCycles == 0))
                {
                    yCycles = ticks;
                }

                if (moons.All(m => Moon.ZComparer.Equals(m, moonMappings[m]) && zCycles == 0))
                {
                    zCycles = ticks;
                }

                if (xCycles * yCycles * zCycles != 0)
                {
                    var result = MathEx.LCM(new long[] { xCycles + 1, yCycles + 1, zCycles + 1 });
                    return(result.ToString());
                }
            }
        }