Advertisement
iliqnvidenov

Untitled

Jan 28th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class BitLock
  6. {
  7.     static void Main()
  8.     {
  9.         int[,] matrix = new int[8,12];
  10.         string inputNums = Console.ReadLine();
  11.         int[] nums = inputNums.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
  12.         List<int> countChecker = new List<int>();
  13.         for (int i = 0; i < matrix.GetLongLength(0); i++)
  14.             {
  15.                 for (int j = (int)matrix.GetLongLength(1)-1,k = 1; j >=0; j--,k*=2)
  16.                     {
  17.                         matrix[i, j] = ToBinary(nums[i]/k);
  18.                     }
  19.             }
  20.         bool notEnd = true;
  21.         while(notEnd)
  22.         {
  23.              string inputOrders = Console.ReadLine();
  24.              string[] orders = inputOrders.Split().ToArray();
  25.              if (orders.Length == 3)
  26.              {
  27.                  matrix = Rotate(orders,matrix);
  28.              }
  29.              if (orders.Length == 2)
  30.              {
  31.                  countChecker.Add(Check(orders, matrix));
  32.              }
  33.              else if(orders.Length == 1)
  34.              {
  35.                  notEnd = false;
  36.              }
  37.         }
  38.  
  39.         // Toint
  40.         List<int> outputNums = new List<int>();
  41.         for (int i = 0; i < matrix.GetLongLength(0); i++)
  42.         {
  43.             int number = 0;
  44.             for (int j = 0, k = (int)matrix.GetLongLength(1)-1 ; j < matrix.GetLongLength(1); j++,k--)
  45.             {
  46.                 number +=matrix[i, j] *((int) Math.Pow(2, k));
  47.             }
  48.                 outputNums.Add(number);
  49.         }
  50.  
  51.         Print(countChecker, outputNums);
  52.     }
  53.     static int ToBinary(int num)
  54.     {
  55.         int bit = 0;
  56.         bit = num % 2;
  57.         return bit;
  58.     }
  59.     static int Check(string[] orders,int[,] matrix)
  60.     {
  61.         int col = int.Parse(orders[1]);
  62.         col = 12 - (col+1);
  63.         int counter = 0;
  64.         for (int i = 0; i < matrix.GetLongLength(0); i++)
  65.         {
  66.             if (matrix[i,col] == 1)
  67.             {
  68.                 counter++;
  69.             }
  70.         }
  71.         return counter;
  72.     }
  73.     static int[,] Rotate(string[] orders,int[,] matrix)
  74.     {
  75.         int row = int.Parse(orders[0]);
  76.         string direction = orders[1];
  77.         int rotations = int.Parse(orders[2]);
  78.         if (direction == "right")
  79.         {
  80.             for (int j = 0; j < rotations; j++)
  81.             {
  82.                 int last = (int)matrix.GetLongLength(1) - 1;
  83.                 for (var i = 0; i < last; i ++)
  84.                 {
  85.                     matrix[row, i] ^= matrix[row, last];
  86.                     matrix[row, last] ^= matrix[row, i];
  87.                     matrix[row, i] ^= matrix[row, last];
  88.                 }
  89.             }
  90.         }
  91.         else if(direction == "left")
  92.         {
  93.             for (int j = 0; j < rotations; j++)
  94.             {
  95.                 int last = (int)matrix.GetLongLength(1) - 1;
  96.                 for (var i = last-1; i >= 0; i--)
  97.                 {
  98.                     matrix[row, i] ^= matrix[row, last];
  99.                     matrix[row,last] ^= matrix[row, i];
  100.                     matrix[row, i] ^= matrix[row, last];
  101.                 }
  102.             }
  103.         }
  104.         return matrix;
  105.     }
  106.     static void Print(List<int> countChecker, List<int> outputNums)
  107.     {
  108.         foreach (var nums in countChecker)
  109.         {
  110.             Console.WriteLine(nums);
  111.         }
  112.         foreach (var number in outputNums)
  113.         {
  114.             Console.Write(number + " ");
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement