// theoretisch gezien werkt het.... public static long Part2Solution(JupiterMoonSystem jupiterMoonSystem, JupiterMoonSystem initialSystem) { long steps = 0; var historyDoesntRepeat = true; var moonEqualityComparer = new MoonEqualityComparer(); var velocityEqualityComparer = new VelocityEqualityComparer(); while (historyDoesntRepeat) { steps++; foreach (var moon in jupiterMoonSystem.GetMoons()) { var otherMoons = jupiterMoonSystem.Moons.Where(m => m != moon); //hier kun je ook excepten als sneller; foreach (var otherMoon in otherMoons) { moon.V.vX += (otherMoon.X - moon.X) == 0 ? (otherMoon.X - moon.X) : (otherMoon.X - moon.X) / Math.Abs(otherMoon.X - moon.X); moon.V.vY += (otherMoon.Y - moon.Y) == 0 ? (otherMoon.Y - moon.Y) : (otherMoon.Y - moon.Y) / Math.Abs(otherMoon.Y - moon.Y); moon.V.vZ += (otherMoon.Z - moon.Z) == 0 ? (otherMoon.Z - moon.Z) : (otherMoon.Z - moon.Z) / Math.Abs(otherMoon.Z - moon.Z); } } foreach (var moon in jupiterMoonSystem.GetMoons()) { moon.Tick(); } if (!initialSystem.Moons.Except(jupiterMoonSystem.Moons, moonEqualityComparer).Any() && !initialSystem.MoonVelocities.Except(jupiterMoonSystem.MoonVelocities, velocityEqualityComparer).Any()) { historyDoesntRepeat = false; } } return(steps); }
static void Main(string[] args) { // Part 1 var jupiterMoonSystem = new JupiterMoonSystem(); jupiterMoonSystem.AddMoon(new Moon(1, 4, 4)); jupiterMoonSystem.AddMoon(new Moon(-4, -1, 19)); jupiterMoonSystem.AddMoon(new Moon(-15, -14, 12)); jupiterMoonSystem.AddMoon(new Moon(-17, 1, 10)); var solution = Part1Solution(jupiterMoonSystem, 1000); System.Console.WriteLine(solution); // Part 2 jupiterMoonSystem = new JupiterMoonSystem(); jupiterMoonSystem.AddMoon(new Moon(1, 4, 4)); jupiterMoonSystem.AddMoon(new Moon(-4, -1, 19)); jupiterMoonSystem.AddMoon(new Moon(-15, -14, 12)); jupiterMoonSystem.AddMoon(new Moon(-17, 1, 10)); var jupiterMoonSystemInitial = new JupiterMoonSystem(); jupiterMoonSystemInitial.AddMoon(new Moon(1, 4, 4)); jupiterMoonSystemInitial.AddMoon(new Moon(-4, -1, 19)); jupiterMoonSystemInitial.AddMoon(new Moon(-15, -14, 12)); jupiterMoonSystemInitial.AddMoon(new Moon(-17, 1, 10)); var solution2 = Part2Solution(jupiterMoonSystem, jupiterMoonSystemInitial); System.Console.WriteLine(solution2); }
public static int Part1Solution(JupiterMoonSystem jupiterMoonSystem, int steps) { for (int t = 0; t < steps; t++) { foreach (var moon in jupiterMoonSystem.GetMoons()) { var otherMoons = jupiterMoonSystem.Moons.Where(m => m != moon); foreach (var otherMoon in otherMoons) { moon.V.vX += (otherMoon.X - moon.X) == 0 ? (otherMoon.X - moon.X) : (otherMoon.X - moon.X) / Math.Abs(otherMoon.X - moon.X); moon.V.vY += (otherMoon.Y - moon.Y) == 0 ? (otherMoon.Y - moon.Y) : (otherMoon.Y - moon.Y) / Math.Abs(otherMoon.Y - moon.Y); moon.V.vZ += (otherMoon.Z - moon.Z) == 0 ? (otherMoon.Z - moon.Z) : (otherMoon.Z - moon.Z) / Math.Abs(otherMoon.Z - moon.Z); } } foreach (var moon in jupiterMoonSystem.GetMoons()) { moon.Tick(); } } return(jupiterMoonSystem.getTotalEnergy()); }