Advertisement
Guest User

Bittris

a guest
Apr 11th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. class Program
  9. {
  10.     private static uint MoveRight(uint[] matrix, int rowCount, uint num)
  11.     {
  12.         uint n = matrix[rowCount];
  13.         if ((((num >> 1) & n) == 0) && num % 2 == 0)
  14.         {
  15.             num = num >> 1;
  16.         }
  17.         return num;
  18.     }
  19.  
  20.     private static uint MoveLeft(uint[] matrix, int rowCount, uint num)
  21.     {
  22.         uint n = matrix[rowCount];
  23.         if ((((num << 1) & n) == 0) && num <= 127)
  24.         {
  25.             num = num << 1;
  26.         }
  27.         return num;
  28.     }
  29.  
  30.     static void Main(string[] args)
  31.     {
  32.         int n = int.Parse(Console.ReadLine());
  33.         uint[] matr = new uint[4];
  34.         ulong output = 0;
  35.         for (int i = 0; i < n / 4; i++)
  36.         {
  37.             uint number = uint.Parse(Console.ReadLine());
  38.             //<fill matrix>
  39.             uint mask = 255;
  40.             uint num = mask & number; //we store the first 8 bits
  41.             uint leadingOnesNum = (~mask) & number; //we store the all the bits except the first 8
  42.             //</fill matrix>
  43.             bool ignoreAll = false;
  44.             for (int j = 0; j < 3; j++) //take 3 commands for each number
  45.             {
  46.                 string command = Console.ReadLine();
  47.                 switch (command)
  48.                 {
  49.                     case "R":
  50.                         num = MoveRight(matr, j, num);
  51.                         break;
  52.                     case "L":
  53.                         num = MoveLeft(matr, j, num);
  54.                         break;
  55.                     case "D":
  56.                         break;
  57.                 }                
  58.                 uint tm = num & matr[j+1];
  59.                 if (ignoreAll == false)
  60.                 {
  61.                     if (tm != 0) //check if the peace is at the bottom
  62.                     {
  63.                         matr[j] = num | matr[j]; //put the peace at the bottom
  64.                         if (matr[j] == 255) //check if it is 255, if it is it calculates output make the row null and move everything down
  65.                         {
  66.                             output += ((ulong)Convert.ToString((leadingOnesNum | num), 2).Count(x => x == '1')) * 10;
  67.                             matr[j] = 0;
  68.                             for (int k = j; k >= 0 ; k--)
  69.                             {
  70.                                 if (k == 0)
  71.                                 {
  72.                                     matr[k] = 0;
  73.                                 }
  74.                                 else
  75.                                 {
  76.                                     matr[k] = matr[k - 1];
  77.                                 }
  78.                             }
  79.                         }
  80.                         else //if it is not 255 only calculate output
  81.                         {
  82.                             output += ((ulong)Convert.ToString((leadingOnesNum | num), 2).Count(x => x == '1'));
  83.                         }
  84.                         ignoreAll = true;
  85.                     }
  86.                     else if (tm == 0 && j == 2) //also checks if the peace is at the bottom, but the bottom of the matrix
  87.                     {
  88.                         matr[j+1] = num | matr[j+1];
  89.                         if (matr[j + 1] == 255)
  90.                         {
  91.                             output += ((ulong)Convert.ToString((leadingOnesNum | num), 2).Count(x => x == '1')) * 10;
  92.                             matr[j + 1] = 0;
  93.                             for (int k = 3; k >= 0; k--)
  94.                             {
  95.                                 if (k == 0)
  96.                                 {
  97.                                     matr[k] = 0;
  98.                                 }
  99.                                 else
  100.                                 {
  101.                                     matr[k] = matr[k - 1];
  102.                                 }
  103.                             }
  104.                         }
  105.                         else
  106.                         {
  107.                             matr[3] = num | matr[3];
  108.                             output += ((ulong)Convert.ToString((leadingOnesNum | num), 2).Count(x => x == '1'));
  109.                         }
  110.                         ignoreAll = true;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         Console.WriteLine(output);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement