iliya87

05.BitLock

Mar 23rd, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2.  
  3. class BitLock
  4. {
  5.     static void Main()
  6.     {
  7.         string[] input = Console.ReadLine().Split();
  8.         int[] lockRows = Array.ConvertAll(input, int.Parse);
  9.  
  10.         string command = Console.ReadLine();
  11.  
  12.         while (command != "end")
  13.         {
  14.             string[] orders = command.Split();
  15.  
  16.             if (orders[0] == "check")
  17.             {
  18.                 int col = int.Parse(orders[1]);
  19.                 int count = 0;
  20.  
  21.                 foreach (var row in lockRows)
  22.                 {
  23.                     count += (row >> col) & 1;
  24.                 }
  25.  
  26.                 Console.WriteLine(count);
  27.             }
  28.             else
  29.             {
  30.                 int row = int.Parse(orders[0]);
  31.                 string direction = orders[1];
  32.                 int rotations = int.Parse(orders[2]) % 12;
  33.  
  34.                 if (direction == "left")
  35.                 {
  36.                     for (int i = 0; i < rotations; i++)
  37.                     {
  38.                         int leftmostBit = (lockRows[row] >> 11) & 1;
  39.                         lockRows[row] &= ~(1 << 11);
  40.                         lockRows[row] <<= 1;
  41.                         lockRows[row] |= leftmostBit;
  42.                     }
  43.                 }
  44.                 else if (direction == "right")
  45.                 {
  46.                     for (int i = 0; i < rotations; i++)
  47.                     {
  48.                         int rightmostBit = lockRows[row] & 1;
  49.                         lockRows[row] >>= 1;
  50.                         lockRows[row] |= rightmostBit << 11;
  51.                     }
  52.                 }
  53.  
  54.             }
  55.  
  56.             command = Console.ReadLine();
  57.         }
  58.  
  59.         foreach (var row in lockRows)
  60.         {
  61.             Console.Write(row + " ");
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment