Advertisement
Filkolev

Cubes

Oct 1st, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. namespace Cubes
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Program
  8.     {
  9.         private static int CubesCount = 0;
  10.         private static readonly HashSet<string> Used = new HashSet<string>();
  11.  
  12.         public static void Main(string[] args)
  13.         {
  14.             var elements = Console.ReadLine()
  15.                 .Split()
  16.                 .ToList();
  17.  
  18.             elements.Sort();
  19.             Permute(elements);
  20.  
  21.             Console.WriteLine(CubesCount);
  22.         }
  23.  
  24.         private static string[] RotateYZ(string[] currentCube)
  25.         {
  26.             string[] temp = new string[12];
  27.  
  28.             temp[0] = currentCube[8];
  29.             temp[1] = currentCube[4];
  30.             temp[2] = currentCube[0];
  31.             temp[3] = currentCube[7];
  32.  
  33.             temp[4] = currentCube[9];
  34.             temp[5] = currentCube[1];
  35.             temp[6] = currentCube[3];
  36.             temp[7] = currentCube[11];
  37.  
  38.             temp[8] = currentCube[10];
  39.             temp[9] = currentCube[5];
  40.             temp[10] = currentCube[2];
  41.             temp[11] = currentCube[6];
  42.  
  43.             return temp;
  44.         }
  45.  
  46.         private static string[] RotateXZ(string[] currentCube)
  47.         {
  48.             string[] temp = new string[12];
  49.  
  50.             temp[0] = currentCube[4];
  51.             temp[1] = currentCube[9];
  52.             temp[2] = currentCube[5];
  53.             temp[3] = currentCube[1];
  54.  
  55.             temp[4] = currentCube[8];
  56.             temp[5] = currentCube[10];
  57.             temp[6] = currentCube[2];
  58.             temp[7] = currentCube[0];
  59.  
  60.             temp[8] = currentCube[7];
  61.             temp[9] = currentCube[11];
  62.             temp[10] = currentCube[6];
  63.             temp[11] = currentCube[3];
  64.  
  65.             return temp;
  66.         }
  67.  
  68.         private static string[] RotateXY(string[] currentCube)
  69.         {
  70.             string[] temp = new string[12];
  71.  
  72.             temp[0] = currentCube[3];
  73.             temp[1] = currentCube[0];
  74.             temp[2] = currentCube[1];
  75.             temp[3] = currentCube[2];
  76.  
  77.             temp[4] = currentCube[7];
  78.             temp[5] = currentCube[4];
  79.             temp[6] = currentCube[5];
  80.             temp[7] = currentCube[6];
  81.  
  82.             temp[8] = currentCube[11];
  83.             temp[9] = currentCube[8];
  84.             temp[10] = currentCube[9];
  85.             temp[11] = currentCube[10];
  86.  
  87.             return temp;
  88.         }
  89.  
  90.         private static void Permute(List<string> array, int start = 0)
  91.         {
  92.             var current = string.Join(",", array);
  93.             MarkUsedCubes(current);
  94.  
  95.             if (!Used.Contains(current))
  96.             {
  97.                 CubesCount++;
  98.             }
  99.  
  100.             if (start < array.Count)
  101.             {
  102.                 for (int i = array.Count - 2; i >= start; i--)
  103.                 {
  104.                     string tmp;
  105.                     for (int j = i + 1; j < array.Count; j++)
  106.                     {
  107.                         if (array[i] != array[j])
  108.                         {
  109.                             // swap
  110.                             tmp = array[i];
  111.                             array[i] = array[j];
  112.                             array[j] = tmp;
  113.  
  114.                             Permute(array, i + 1);
  115.                         }
  116.                     }
  117.  
  118.                     // Undo all modifications done by recursive calls and swapping
  119.                     tmp = array[i];
  120.                     for (int k = i; k < array.Count - 1; k++)
  121.                     {
  122.                         array[k] = array[k + 1];
  123.                     }
  124.  
  125.                     array[array.Count - 1] = tmp;
  126.                 }
  127.             }
  128.         }
  129.  
  130.         private static void MarkUsedCubes(string check)
  131.         {
  132.             var currentCube = check.Split(',').ToArray();
  133.  
  134.             for (int j = 0; j < 4; j++)
  135.             {
  136.                 currentCube = RotateXY(currentCube);
  137.                 for (int k = 0; k < 4; k++)
  138.                 {
  139.                     currentCube = RotateXZ(currentCube);
  140.                     for (int l = 0; l < 4; l++)
  141.                     {
  142.                         currentCube = RotateYZ(currentCube);
  143.  
  144.                         var rotatedEquivalent = string.Join(",", currentCube);
  145.  
  146.                         if (rotatedEquivalent != check)
  147.                         {
  148.                             Used.Add(rotatedEquivalent);
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement