code_junkie

Algorithm: Odometer / Brute force

Nov 11th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. protected void Main()
  2. {
  3. char[] chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  4. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
  5.  
  6. BruteForce(chars, 5);
  7. }
  8.  
  9. private void BruteForce(char[] chars, int length)
  10. {
  11. // for-loop (?) console-writing all possible combinations from 00000 to JJJJJ
  12. // (when passed in length is 5)
  13. // TODO: Implement code...
  14. }
  15.  
  16. private static IEnumerable<string> GetAllMatches(char[] chars, int length)
  17. {
  18. int[] indexes = new int[length];
  19. char[] current = new char[length];
  20. for (int i=0; i < length; i++)
  21. {
  22. current[i] = chars[0];
  23. }
  24. do
  25. {
  26. yield return new string(current);
  27. }
  28. while (Increment(indexes, current, chars));
  29. }
  30.  
  31. private static bool Increment(int[] indexes, char[] current, char[] chars)
  32. {
  33. int position = indexes.Length-1;
  34.  
  35. while (position >= 0)
  36. {
  37. indexes[position]++;
  38. if (indexes[position] < chars.Length)
  39. {
  40. current[position] = chars[indexes[position]];
  41. return true;
  42. }
  43. indexes[position] = 0;
  44. current[position] = chars[0];
  45. position--;
  46. }
  47. return false;
  48. }
  49.  
  50. public static void PerformBruteForce() {
  51. char[] characters = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  52. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
  53.  
  54. int length = 5; // Length of the string created by bruteforce
  55. int charactersLength = characters.Length;
  56. int[] odometer = new int[length];
  57. long size = (long)Math.Pow(charactersLength, length);
  58.  
  59. for (int i = 0; i < size; i++) {
  60. WriteBruteForce(odometer, characters);
  61. int position = 0;
  62. do {
  63. odometer[position] += 1;
  64. odometer[position] %= charactersLength;
  65. } while (odometer[position++] == 0 && position < length);
  66. }
  67. }
  68.  
  69. private static void WriteBruteForce(int[] odometer, char[] characters) {
  70. // Print backwards
  71. for (int i = odometer.Length - 1; i >= 0; i--) {
  72. Console.Write(characters[odometer[i]]);
  73. }
  74. Console.WriteLine();
  75. }
  76.  
  77. for (int i = 0; i < (1 << 24); i++)
  78. string s = i.ToString("X6");
  79.  
  80. public class BaseNCounter
  81. {
  82. public char[] CharSet { get; set; }
  83. public int Power { get; set; }
  84.  
  85. public BaseNCounter() { }
  86.  
  87. public IEnumerable<string> Count() {
  88. long max = (long)Math.Pow((double)this.CharSet.Length, (double)this.Power);
  89. long[] counts = new long[this.Power];
  90. for(long i = 0; i < max; i++)
  91. yield return IncrementArray(ref counts, i);
  92. }
  93.  
  94. public string IncrementArray(ref long[] counts, long count) {
  95. long temp = count;
  96. for (int i = this.Power - 1; i >= 0 ; i--) {
  97. long pow = (long)Math.Pow(this.CharSet.Length, i);
  98. counts[i] = temp / pow;
  99. temp = temp % pow;
  100. }
  101.  
  102. StringBuilder sb = new StringBuilder();
  103. foreach (int c in counts) sb.Insert(0, this.CharSet[c]);
  104. return sb.ToString();
  105. }
  106. }
  107.  
  108. class Program
  109. {
  110. static void Main(string[] args)
  111. {
  112. BaseNCounter c = new BaseNCounter() {
  113. CharSet = new char [] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' },
  114. Power = 2};
  115.  
  116. foreach(string cc in c.Count())
  117. Console.Write("{0} ,", cc);
  118. Console.WriteLine("");
  119.  
  120. BaseNCounter c2 = new BaseNCounter()
  121. {
  122. CharSet = new char[] { 'x', 'q', 'r', '9'},
  123. Power = 3
  124. };
  125. foreach (string cc in c2.Count())
  126. Console.Write("{0} ,", cc);
  127. Console.Read();
  128. }
  129. }
Add Comment
Please, Sign In to add comment