Advertisement
_CodeBehind

Rubik

Jun 1st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class RubiksMatrix
  5. {
  6.     public static void Main()
  7.     {
  8.         var rubicParams = Console.ReadLine()
  9.             .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  10.             .Select(int.Parse)
  11.             .ToArray();
  12.  
  13.         var numberOfCommands = int.Parse(Console.ReadLine());
  14.  
  15.         var rubic = new int[rubicParams[0]][];
  16.         var counter = 1;
  17.  
  18.         for (int row = 0; row < rubic.GetLength(0); row++)
  19.         {
  20.             rubic[row] = new int[rubicParams[1]];
  21.  
  22.             for (int col = 0; col < rubic[row].Length; col++)
  23.             {
  24.                 rubic[row][col] = counter++;
  25.             }
  26.         }
  27.  
  28.         for (int i = 0; i < numberOfCommands; i++)
  29.         {
  30.             var currentCommand = Console.ReadLine().Split().ToArray();
  31.  
  32.             var startIndex = int.Parse(currentCommand[0]);
  33.             var direction = currentCommand[1];
  34.             var moves = int.Parse(currentCommand[2]);
  35.  
  36.             switch (direction)
  37.             {
  38.                 case "up":
  39.                     MoveUp(rubic, startIndex, moves);
  40.                     break;
  41.                 case "down":
  42.                     MoveDown(rubic, startIndex, moves);
  43.                     break;
  44.                 case "left":
  45.                     MoveLeft(rubic, startIndex, moves);
  46.                     break;
  47.                 case "right":
  48.                     MoveRight(rubic, startIndex, moves);
  49.                     break;
  50.             }
  51.         }
  52.  
  53.         int numberToCheck = 1;
  54.  
  55.         for (int row = 0; row < rubic.Length; row++)
  56.         {
  57.             for (int col = 0; col < rubic.Length; col++)
  58.             {
  59.                 if (rubic[row][col] == numberToCheck)
  60.                 {
  61.                     Console.WriteLine("No swap required");
  62.                 }
  63.                 else
  64.                 {
  65.                     int swapRow = 0;
  66.                     int swapCol = 0;
  67.  
  68.                     for (int r = row; r < rubic.Length; r++)
  69.                     {
  70.                         for (int c = 0; c < rubic.Length; c++)
  71.                         {
  72.                             if (rubic[r][c] == numberToCheck)
  73.                             {
  74.                                 swapRow = r;
  75.                                 swapCol = c;
  76.                                 int tempElement = rubic[r][c];
  77.                                 rubic[r][c] = rubic[row][col];
  78.                                 rubic[row][col] = tempElement;
  79.                             }
  80.                         }
  81.                     }
  82.  
  83.                     Console.WriteLine($"Swap ({row}, {col}) with ({swapRow}, {swapCol})");
  84.                 }
  85.  
  86.                 numberToCheck++;
  87.             }
  88.         }
  89.     }
  90.  
  91.     private static void MoveRight(int[][] rubic, int startIndex, int moves)
  92.     {
  93.         for (int iterations = 0; iterations < moves % rubic.Length; iterations++)
  94.         {
  95.             for (int col = rubic.Length - 1; col > 0; col--)
  96.             {
  97.                 var temp = rubic[startIndex][col];
  98.                 var next = rubic[startIndex][col - 1];
  99.                 rubic[startIndex][col - 1] = temp;
  100.                 rubic[startIndex][col] = next;
  101.             }
  102.         }
  103.     }
  104.  
  105.     private static void MoveLeft(int[][] rubic, int startIndex, int moves)
  106.     {
  107.         for (int iterations = 0; iterations < moves % rubic.Length; iterations++)
  108.         {
  109.             for (int col = 0; col < rubic.Length - 1; col++)
  110.             {
  111.                 var temp = rubic[startIndex][col];
  112.                 rubic[startIndex][col] = rubic[startIndex][col + 1];
  113.                 rubic[startIndex][col + 1] = temp;
  114.             }
  115.         }
  116.     }
  117.  
  118.     private static void MoveDown(int[][] rubic, int startIndex, int moves)
  119.     {
  120.         for (int iterations = 0; iterations < moves % rubic.Length; iterations++)
  121.         {
  122.             for (int row = rubic.Length - 1; row > 0; row--)
  123.             {
  124.                 var temp = rubic[row][startIndex];
  125.                 rubic[row][startIndex] = rubic[row - 1][startIndex];
  126.                 rubic[row - 1][startIndex] = temp;
  127.             }
  128.         }
  129.     }
  130.  
  131.     private static void MoveUp(int[][] rubik, int startIndex, int moves)
  132.     {
  133.         for (int iterations = 0; iterations < moves % rubik.Length; iterations++)
  134.         {
  135.             for (int row = 0; row < rubik.Length - 1; row++)
  136.             {
  137.                 var temp = rubik[row][startIndex];
  138.                 rubik[row][startIndex] = rubik[row + 1][startIndex];
  139.                 rubik[row + 1][startIndex] = temp;
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement