public static long Problem1(IEnumerable <long> input) { var possibleCombinations = Permutations.GetPermutations("01234"); var instructionSet = GetInstructionSet(); var computer = new IntCodeComputer(instructionSet); var bestResult = long.MinValue; foreach (var combination in possibleCombinations) { ProgramOutput = new Queue <long>(); ProgramInput = new Queue <long>(); ProgramOutput.Enqueue(0); for (int i = 0; i < 5; i++) { ProgramInput.Enqueue(int.Parse(combination[i].ToString())); var output = ProgramOutput.Dequeue(); ProgramInput.Enqueue(output); computer.LoadMemory(input.ToList()); computer.Evaluate(); } bestResult = Math.Max(ProgramOutput.Dequeue(), bestResult); } return(bestResult); }
public static long Problem2(IEnumerable <long> input) { var possibleCombinations = Permutations.GetPermutations("56789"); var instructionSet = GetInstructionSet(); //Change the output to interrupt the current program instructionSet.First(instruction => instruction.OpCode == 4).Sub = (parameters, executionContext) => { executionContext.Interrupted = true; ProgramOutput.Enqueue(parameters[0]); }; var bestResult = long.MinValue; foreach (var combination in possibleCombinations) { ProgramOutput = new Queue <long>(); ProgramInput = new Queue <long>(); var amplifiers = new IntCodeComputer[5]; for (var i = 0; i < 5; i++) { amplifiers[i] = new IntCodeComputer(instructionSet); amplifiers[i].LoadMemory(input.ToList()); } var executionResult = ExecutionState.Interrupted; for (var turn = 0; executionResult != ExecutionState.Finished; turn++) { if (turn < 5) { ProgramInput.Enqueue(int.Parse(combination[turn % 5].ToString())); } if (turn == 0) { ProgramOutput.Enqueue(0); } var output = ProgramOutput.Dequeue(); ProgramInput.Enqueue(output); executionResult = amplifiers[turn % 5].Evaluate(); } bestResult = Math.Max(ProgramInput.Dequeue(), bestResult); } return(bestResult); }
public static void Main(string[] args) { Console.WriteLine("Running!"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); int largest = 1; // Index referencing largest (i.e. first) value found int digits = 9; // Number of digits in the sequence List <int> panSeq = new List <int> (digits); // The pandigital sequence for (int n = 1; n <= digits; n++) // Populate based on number of digits { panSeq.Add(n); } Permutations permutations = new Permutations(panSeq); // Instantiate object permutations.GetPermutations(); // Generate the permutations for (int i = permutations.Count - 1; i >= 0; i--) // For each permutation, starting at the end { List <int> permutation = permutations.GetPermutation(i); // Grab it from the object if (!TestOne(permutation) && // Test all the configurations !TestTwo(permutation) && !TestThree(permutation) && !TestFour(permutation)) { continue; // Didn't pass any; move onto the next } else // One of them passed { largest = i; // Set the index that passed break; } } // Grab the permutation using the index and turn it into an integer for printout List <int> largestPermutation = permutations.GetPermutation(largest); StringBuilder largestString = new StringBuilder(); foreach (int digit in largestPermutation) { largestString.Append(digit); } int largestVal = Int32.Parse(largestString.ToString()); stopWatch.Stop(); Console.WriteLine("{0} found in {1} milliseconds", largestVal, stopWatch.ElapsedMilliseconds); }
public static void Main(string[] args) { Console.WriteLine("Running!"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); int largest = 1; int digits = 9; List <int> panSeq = new List <int> (digits); for (int n = 1; n <= digits; n++) { panSeq.Add(n); } Permutations permutations = new Permutations(panSeq); permutations.GetPermutations(); for (int i = permutations.Count - 1; i >= 0; i--) { List <int> permutation = permutations.GetPermutation(i); if (!TestOne(permutation) && !TestTwo(permutation) && !TestThree(permutation) && !TestFour(permutation)) { continue; } else { largest = i; break; } } List <int> largestPermutation = permutations.GetPermutation(largest); StringBuilder largestString = new StringBuilder(); foreach (int digit in largestPermutation) { largestString.Append(digit); } int largestVal = Int32.Parse(largestString.ToString()); stopWatch.Stop(); Console.WriteLine("{0} found in {1} milliseconds", largestVal, stopWatch.ElapsedMilliseconds); }
public void TestPermutationEnumerator() { int[] testArrayLengths = new int[] { 1, 3, 4, 7 }; int[] seeds = new int[] { 1, 54353, 6363, 756757 }; /* * Instantiate and seed random number generator within * method to keep array elements consistent between tests */ Random arrayNumberGenerator = new Random(2017); foreach (int arrayLength in testArrayLengths) { //foreach (int seed in seeds) //{ //generate random array of given lenth int[] array = new int[arrayLength]; for (int i = 0; i < arrayLength; ++i) { array[i] = i + 1; } int[][] permutations = Permutations.GetPermutations <int>(array); PermutationEnumerator <int> enumerator = new PermutationEnumerator <int>(array); int currentPermutation = 0; foreach (int [] currentEnumeratorPermutation in enumerator) { Assert.AreEqual(currentEnumeratorPermutation.Length, permutations[currentPermutation].Length); for (int j = 0; j < currentEnumeratorPermutation.Length; ++j) { Assert.AreEqual(currentEnumeratorPermutation[j], permutations[currentPermutation][j]); } ++currentPermutation; } } }
private void GridForm_Load(object sender, EventArgs e) { List <List <int> > permList = new List <List <int> >(); if (grid.InitialPointsList.Count <= 5) { IEnumerable <IEnumerable <int> > result = Permutations.GetPermutations(Enumerable.Range(1, grid.InitialPointsList.Count), grid.InitialPointsList.Count); foreach (var list in result) { var row = new List <int>(); foreach (var elem in list) { row.Add(elem); } permList.Add(row); } } SetGridUi(); var gridCpy = new Grid(grid.CopyList()); if (grid.InitialPointsList.Count > 5) { for (int i = 1; i < grid.InitialPointsList.Count; i++) { if (i == 5) { Console.WriteLine(gridCpy.MakeGridDirty(5)); } GetPaths(gridCpy, i); } this.grid = gridCpy; this.UpdateGridButtons(); } else { int permListIndex = 0; while (!gridCpy.isCorrect() && permListIndex < permList.Count - 1) { Console.WriteLine($"index {permListIndex} out of {permList.Count}"); gridCpy = new Grid(grid.CopyList()); foreach (var nr in permList[permListIndex]) { bool hasPath = GetPaths(gridCpy, nr); if (!hasPath) { break; } } permListIndex++; } if (gridCpy.isCorrect()) { this.grid = gridCpy; this.UpdateGridButtons(); } } }
public static void Main(string[] args) { Console.WriteLine("Running!"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // Get all the pandigital permutations of nine digits int digits = 9; Permutations permutations = new Permutations(digits); permutations.GetPermutations(); // Generate the primes up to 17 List <int> primes = new List <int> (); primes.Add(2); Boolean isPrime; for (int p = 3; p <= 17; p++) { isPrime = true; for (int i = 0; primes [i] * primes [i] <= p; i++) { if (p % primes [i] == 0) { isPrime = false; break; } } if (isPrime) { primes.Add(p); } } List <int> permutation = new List <int> (); int offset; int value; bool isDivisible; double sum = 0; double permutationValue; double multiple; for (int i = 0; i < permutations.Count; i++) { permutation = permutations.GetPermutation(i); isDivisible = true; // Quick test for divisibility by 2 and 5 if (permutation [3] % 2 != 0) { continue; } if (permutation [5] != 5 && permutation [5] != 0) { continue; } for (int p = 6; p >= 0; p--) { offset = 6 - p; value = permutation [9 - offset] + (10 * permutation [8 - offset]) + (100 * permutation [7 - offset]); if (value % primes [p] == 0) { continue; } else { isDivisible = false; break; } } if (isDivisible) { permutationValue = 0; for (int o = 1; o <= 10; o++) { multiple = (int)Math.Pow(10, o - 1); permutationValue += permutation [10 - o] * multiple; } sum += permutationValue; } } stopWatch.Stop(); Console.WriteLine("{0} found in {1} milliseconds", sum, stopWatch.ElapsedMilliseconds); }
public void TestPermutations() { int[] testArrayLengths = new int[] { 1, 3, 4, 7 }; int[] seeds = new int[] { 1, 54353, 6363, 756757 }; /* * Instantiate and seed random number generator within * method to keep array elements consistent between tests */ Random arrayNumberGenerator = new Random(2017); foreach (int arrayLength in testArrayLengths) { //foreach (int seed in seeds) //{ //generate random array of given lenth int[] array = new int[arrayLength]; for (int i = 0; i < arrayLength; ++i) { array[i] = i + 1; } int[][] permutations = Permutations.GetPermutations <int>(array); Array.Sort(permutations, new ArrayComparer <int>()); /* * First test: All array elements should be unique */ foreach (int[] permutation in permutations) { for (int index = 0; index < permutation.Length; ++index) { Assert.IsTrue(Array.FindAll(permutation, (int item) => (item == permutation[index])).Length == 1); } } /* * Second test: Only two elements should change between * iterations of the method. Ensure that only two elements * have changed positions since the last iteration, for every * iteration */ for (int i = 1; i < permutations.Length; ++i) { int[] currentPermutation = permutations[i]; int[] previousPermutation = permutations[i - 1]; int differences = ArraySimilarity(currentPermutation, previousPermutation); Assert.IsTrue(differences > 0, $"Current permutation {string.Join(",", currentPermutation)}, Last Permutation {string.Join(", ", previousPermutation)}, differences = {differences}"); } /* * Create count of how many times a given value occurs at a given position * If the permutation algorithm run correctly, each value occurs at each * position in the array an equal number of times. */ int[][] counts = new int[arrayLength][]; for (int row = 0; row < arrayLength; ++row) { counts[row] = new int[arrayLength]; } foreach (int[] permutation in permutations) { for (int index = 0; index < permutation.Length; ++index) { ++counts[index][permutation[index] - 1]; } } for (int n = 0; n < arrayLength * arrayLength - 1; ++n) { Assert.AreEqual(counts[n / arrayLength][n % arrayLength], counts[(n + 1) / arrayLength][(n + 1) % arrayLength]); } } }