Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class Cubes
- {
- private static HashSet<string> invalidCombs = new HashSet<string>();
- private static HashSet<string> combs = new HashSet<string>();
- private static string[, ,] cube;
- public static void Main()
- {
- string[] stickValues = Console.ReadLine().Split(' ');
- PermuteWithRepetitions(stickValues, 0, stickValues.Length - 1);
- Console.WriteLine(combs.Count);
- }
- private static void PermuteWithRepetitions(string[] stickValues, int start, int end)
- {
- AddCurrentCombinationToList(stickValues);
- for (int left = end - 1; left >= start; left--)
- {
- for (int right = left + 1; right <= end; right++)
- {
- if (stickValues[left] != stickValues[right])
- {
- Swap(ref stickValues[left], ref stickValues[right]);
- PermuteWithRepetitions(stickValues, left + 1, end);
- }
- }
- var firstElement = stickValues[left];
- for (int i = left; i <= end - 1; i++)
- {
- stickValues[i] = stickValues[i + 1];
- }
- stickValues[end] = firstElement;
- }
- }
- private static void AddCurrentCombinationToList(string[] currentCombination)
- {
- AssignCubeValues(currentCombination);
- RotateCube();
- if (!invalidCombs.Contains(string.Join("", currentCombination)))
- {
- combs.Add(string.Join("", currentCombination));
- }
- }
- private static void Swap<T>(ref T first, ref T second)
- {
- T oldFirst = first;
- first = second;
- second = oldFirst;
- }
- private static void AssignCubeValues(string[] stickValues)
- {
- cube = new string[3, 2, 2];
- int index = 0;
- for (int z = 0; z < cube.GetLength(2); z++)
- {
- for (int y = 0; y < cube.GetLength(1); y++)
- {
- for (int x = 0; x < cube.GetLength(0); x++)
- {
- cube[x, y, z] = stickValues[index];
- index++;
- }
- }
- }
- }
- private static void RotateCube()
- {
- for (int i = 0; i < 3; i++)
- {
- RotateByAxis(cube, 0, 0, 0);
- RotateByAxis(cube, 1, 0, 0);
- RotateByAxis(cube, 2, 0, 0);
- invalidCombs.Add(GetCurrentComb(cube));
- }
- }
- private static void RotateByAxis(string[, ,] cube, int x, int y, int z)
- {
- string oldValue = cube[x, y, z];
- cube[x, y, z] = cube[x, y, z + 1];
- cube[x, y, z + 1] = cube[x, y + 1, z + 1];
- cube[x, y + 1, z + 1] = cube[x, y + 1, z];
- cube[x, y + 1, z] = oldValue;
- }
- private static string GetCurrentComb(string[, ,] cube)
- {
- StringBuilder result = new StringBuilder();
- for (int z = 0; z < cube.GetLength(2); z++)
- {
- for (int y = 0; y < cube.GetLength(1); y++)
- {
- for (int x = 0; x < cube.GetLength(0); x++)
- {
- result.Append(cube[x, y, z]);
- }
- }
- }
- return result.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement