diff --git a/Day12/Day12.cs b/Day12/Day12.cs index 9d7585a..f29b759 100644 --- a/Day12/Day12.cs +++ b/Day12/Day12.cs @@ -29,16 +29,36 @@ namespace Advent public Coordinates Position { get; } public Coordinates Velocity { get; } + + public string GetState() + { + return $"{Position.Z},{Velocity.Z}"; + } } public static void Execute() { + /* + + + + + */ + + //var moons = new List + //{ + // new Moon(-1, 0, 2) , + // new Moon(2, -10, -7), + // new Moon(4, -8, 8 ), + // new Moon(3, 5, -1 ) + //}; + /* - */ + */ var moons = new List { @@ -48,7 +68,14 @@ namespace Advent new Moon(-16, 4, 2 ) }; - for (var count = 1; count <= 1000; count++) + //Console.WriteLine(lcm_of_array_elements(new long[] { 113028, 167624, 231614 })); + //return; + + var previousStates = new Dictionary(); + + var index = 1; + + while (true) { for (var a = 0; a <= 3; a++) { @@ -98,19 +125,98 @@ namespace Advent moon.Position.Y += moon.Velocity.Y; moon.Position.Z += moon.Velocity.Z; } + + var states = new List(); + + foreach (var moon in moons) + states.Add(moon.GetState()); + + var totalState = string.Join(':', states.ToArray()); + + if (previousStates.ContainsKey(totalState)) + { + Console.WriteLine(index - previousStates[totalState]); + break; + } + else + { + previousStates[totalState] = index++; + } } - var totalEnergy = 0; + //var totalEnergy = 0; - foreach (var moon in moons) + //foreach (var moon in moons) + //{ + // var potentialEnergy = Math.Abs(moon.Position.X) + Math.Abs(moon.Position.Y) + Math.Abs(moon.Position.Z); + // var kineticEnergy = Math.Abs(moon.Velocity.X) + Math.Abs(moon.Velocity.Y) + Math.Abs(moon.Velocity.Z); + + // totalEnergy += (potentialEnergy * kineticEnergy); + //} + + //Console.WriteLine(totalEnergy); + } + + private static long lcm_of_array_elements(long[] element_array) + { + long lcm_of_array_elements = 1; + int divisor = 2; + + while (true) { - var potentialEnergy = Math.Abs(moon.Position.X) + Math.Abs(moon.Position.Y) + Math.Abs(moon.Position.Z); - var kineticEnergy = Math.Abs(moon.Velocity.X) + Math.Abs(moon.Velocity.Y) + Math.Abs(moon.Velocity.Z); - totalEnergy += (potentialEnergy * kineticEnergy); + int counter = 0; + bool divisible = false; + for (int i = 0; i < element_array.Length; i++) + { + + // lcm_of_array_elements (n1, n2, ... 0) = 0. + // For negative number we convert into + // positive and calculate lcm_of_array_elements. + if (element_array[i] == 0) + { + return 0; + } + else if (element_array[i] < 0) + { + element_array[i] = element_array[i] * (-1); + } + if (element_array[i] == 1) + { + counter++; + } + + // Divide element_array by devisor if complete + // division i.e. without remainder then replace + // number with quotient; used for find next factor + if (element_array[i] % divisor == 0) + { + divisible = true; + element_array[i] = element_array[i] / divisor; + } + } + + // If divisor able to completely divide any number + // from array multiply with lcm_of_array_elements + // and store into lcm_of_array_elements and continue + // to same divisor for next factor finding. + // else increment divisor + if (divisible) + { + lcm_of_array_elements = lcm_of_array_elements * divisor; + } + else + { + divisor++; + } + + // Check if all element_array is 1 indicate + // we found all factors and terminate while loop. + if (counter == element_array.Length) + { + return lcm_of_array_elements; + } } - - Console.WriteLine(totalEnergy); } } }