Advertisement
Guest User

Cubes

a guest
Apr 15th, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class Cubes
  7. {
  8. private static HashSet<string> invalidCombs = new HashSet<string>();
  9. private static HashSet<string> combs = new HashSet<string>();
  10. private static string[, ,] cube;
  11.  
  12. public static void Main()
  13. {
  14. string[] stickValues = Console.ReadLine().Split(' ');
  15.  
  16.  
  17. PermuteWithRepetitions(stickValues, 0, stickValues.Length - 1);
  18.  
  19. Console.WriteLine(combs.Count);
  20. }
  21.  
  22. private static void PermuteWithRepetitions(string[] stickValues, int start, int end)
  23. {
  24. AddCurrentCombinationToList(stickValues);
  25.  
  26. for (int left = end - 1; left >= start; left--)
  27. {
  28. for (int right = left + 1; right <= end; right++)
  29. {
  30. if (stickValues[left] != stickValues[right])
  31. {
  32. Swap(ref stickValues[left], ref stickValues[right]);
  33. PermuteWithRepetitions(stickValues, left + 1, end);
  34. }
  35. }
  36.  
  37. var firstElement = stickValues[left];
  38. for (int i = left; i <= end - 1; i++)
  39. {
  40. stickValues[i] = stickValues[i + 1];
  41. }
  42.  
  43. stickValues[end] = firstElement;
  44. }
  45. }
  46.  
  47. private static void AddCurrentCombinationToList(string[] currentCombination)
  48. {
  49. AssignCubeValues(currentCombination);
  50. RotateCube();
  51.  
  52. if (!invalidCombs.Contains(string.Join("", currentCombination)))
  53. {
  54. combs.Add(string.Join("", currentCombination));
  55. }
  56. }
  57.  
  58. private static void Swap<T>(ref T first, ref T second)
  59. {
  60. T oldFirst = first;
  61. first = second;
  62. second = oldFirst;
  63. }
  64.  
  65. private static void AssignCubeValues(string[] stickValues)
  66. {
  67. cube = new string[3, 2, 2];
  68. int index = 0;
  69. for (int z = 0; z < cube.GetLength(2); z++)
  70. {
  71. for (int y = 0; y < cube.GetLength(1); y++)
  72. {
  73. for (int x = 0; x < cube.GetLength(0); x++)
  74. {
  75. cube[x, y, z] = stickValues[index];
  76. index++;
  77. }
  78. }
  79. }
  80. }
  81.  
  82. private static void RotateCube()
  83. {
  84. for (int i = 0; i < 3; i++)
  85. {
  86. RotateByAxis(cube, 0, 0, 0);
  87. RotateByAxis(cube, 1, 0, 0);
  88. RotateByAxis(cube, 2, 0, 0);
  89. invalidCombs.Add(GetCurrentComb(cube));
  90. }
  91. }
  92.  
  93. private static void RotateByAxis(string[, ,] cube, int x, int y, int z)
  94. {
  95. string oldValue = cube[x, y, z];
  96. cube[x, y, z] = cube[x, y, z + 1];
  97. cube[x, y, z + 1] = cube[x, y + 1, z + 1];
  98. cube[x, y + 1, z + 1] = cube[x, y + 1, z];
  99. cube[x, y + 1, z] = oldValue;
  100. }
  101.  
  102. private static string GetCurrentComb(string[, ,] cube)
  103. {
  104. StringBuilder result = new StringBuilder();
  105. for (int z = 0; z < cube.GetLength(2); z++)
  106. {
  107. for (int y = 0; y < cube.GetLength(1); y++)
  108. {
  109. for (int x = 0; x < cube.GetLength(0); x++)
  110. {
  111. result.Append(cube[x, y, z]);
  112. }
  113. }
  114. }
  115.  
  116. return result.ToString();
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement