Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _08.Cube
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class CubeSolution
- {
- // partial solution - 20pts in judge
- static HashSet<int[]> cubeCombinations = new HashSet<int[]>();
- private static int output = 1;
- static void Main(string[] args)
- {
- int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- cubeCombinations = GenerateAllCubeRotations(input);
- GenerateAllPermutations(input, 0);
- Console.WriteLine(output);
- }
- private static HashSet<int[]> GenerateAllCubeRotations(int[] input)
- {
- var allCubeCombinations = new HashSet<int[]>();
- var cube = new Cube(input);
- for (int i = 0; i < 6; i++)
- {
- if (i < 4)
- {
- cube.FlipFrontCubesToRight();
- }
- else if (i == 4)
- {
- cube.FlipFrontCubesUp();
- }
- else
- {
- cube.FlipFrontCubesUp();
- cube.FlipFrontCubesUp();
- }
- for (int j = 0; j < 4; j++)
- {
- cube.RotateCube();
- allCubeCombinations.Add(cube.GetCubeStics());
- }
- }
- return allCubeCombinations;
- }
- static void GenerateAllPermutations(int[] arr, int k)
- {
- if (k >= arr.Length)
- {
- bool areEqual = CompareCubes(arr);
- if (!areEqual)
- {
- output++;
- }
- }
- else
- {
- var swapped = new HashSet<int>();
- for (int i = k; i < arr.Length; i++)
- {
- if (!swapped.Contains(arr[i]))
- {
- Swap(ref arr[k], ref arr[i]);
- GenerateAllPermutations(arr, k + 1);
- Swap(ref arr[k], ref arr[i]);
- swapped.Add(arr[i]);
- }
- }
- }
- }
- private static bool CompareCubes(int[] arr)
- {
- var arrayCubeRotations = GenerateAllCubeRotations(arr);
- foreach (var cubeCombination in cubeCombinations)
- {
- foreach (var arrayCubeRotation in arrayCubeRotations)
- {
- if (cubeCombination.SequenceEqual(arrayCubeRotation))
- {
- return true;
- }
- }
- }
- return false;
- }
- static void Swap(ref int first, ref int second)
- {
- int oldFirst = first;
- first = second;
- second = oldFirst;
- }
- static int[] ShiftRight(int[] arr)
- {
- int[] shifted = new int[arr.Length];
- for (int i = 0; i < arr.Length; i++)
- {
- shifted[i] = arr[i == 0 ? (arr.Length - 1) : (i - 1)];
- }
- return shifted;
- }
- internal class Cube
- {
- const int edges = 12;
- public Square FrontSquare { get; set; }
- public Square UpSquare { get; set; }
- public Square RightSquare { get; set; }
- public Square LEftSquare { get; set; }
- public Square DownSquare { get; set; }
- public Square BackSquare { get; set; }
- public Cube(int[] cubeValues)
- {
- this.FrontSquare = new Square(cubeValues[0], cubeValues[1], cubeValues[2], cubeValues[3]);
- this.UpSquare = new Square(cubeValues[4], cubeValues[5], cubeValues[6], cubeValues[1]);
- this.RightSquare = new Square(cubeValues[2], cubeValues[6], cubeValues[7], cubeValues[8]);
- this.LEftSquare = new Square(cubeValues[9], cubeValues[4], cubeValues[0], cubeValues[10]);
- this.DownSquare = new Square(cubeValues[10], cubeValues[3], cubeValues[8], cubeValues[11]);
- this.BackSquare = new Square(cubeValues[9], cubeValues[11], cubeValues[7], cubeValues[5]);
- }
- public void RotateCube()
- {
- this.FrontSquare.RotateSquareOpposite();
- this.BackSquare.RotateSquare();
- this.FlipSideCubeSidesToRight();
- }
- public override string ToString()
- {
- var sticks = this.GetCubeStics();
- return String.Join(" ", sticks);
- }
- public void FlipFrontCubesToRight()
- {
- var spare = this.FrontSquare;
- this.FrontSquare = this.LEftSquare;
- this.LEftSquare = this.BackSquare;
- this.LEftSquare.RotateSquare();
- this.LEftSquare.RotateSquare();
- this.BackSquare = this.RightSquare;
- this.BackSquare.RotateSquare();
- this.BackSquare.RotateSquare();
- this.RightSquare = spare;
- this.UpSquare.RotateSquare();
- this.DownSquare.RotateSquareOpposite();
- }
- public void FlipFrontCubesUp()
- {
- var spare = this.FrontSquare;
- this.FrontSquare = this.UpSquare;
- this.UpSquare = this.BackSquare;
- this.BackSquare = this.DownSquare;
- this.DownSquare = spare;
- this.LEftSquare.RotateSquareOpposite();
- this.RightSquare.RotateSquare();
- }
- public void FlipSideCubeSidesToRight()
- {
- var spareSquare = this.UpSquare;
- this.UpSquare = this.LEftSquare;
- this.UpSquare.RotateSquareOpposite();
- this.LEftSquare = this.DownSquare;
- this.LEftSquare.RotateSquareOpposite();
- this.DownSquare = this.RightSquare;
- this.DownSquare.RotateSquareOpposite();
- this.RightSquare = spareSquare;
- this.RightSquare.RotateSquareOpposite();
- }
- public int[] GetCubeStics()
- {
- var result = new int[12];
- result[0] = this.FrontSquare.a;
- result[1] = this.FrontSquare.b;
- result[2] = this.FrontSquare.c;
- result[3] = this.FrontSquare.d;
- result[4] = this.UpSquare.a;
- result[5] = this.UpSquare.b;
- result[6] = this.UpSquare.c;
- result[7] = this.RightSquare.c;
- result[8] = this.RightSquare.d;
- result[9] = this.LEftSquare.a;
- result[10] = this.LEftSquare.d;
- result[11] = this.BackSquare.b;
- return result;
- }
- }
- internal class Square
- {
- public int a { get; set; }
- public int b { get; set; }
- public int c { get; set; }
- public int d { get; set; }
- public Square(int a, int b, int c, int d)
- {
- this.a = a;
- this.b = b;
- this.c = c;
- this.d = d;
- }
- public void RotateSquare()
- {
- var spareA = this.a;
- this.a = this.b;
- this.b = this.c;
- this.c = this.d;
- this.d = spareA;
- }
- public void RotateSquareOpposite()
- {
- var spareA = this.a;
- this.a = this.d;
- this.d = this.c;
- this.c = this.b;
- this.b = spareA;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement