Advertisement
Guest User

Problem 5 – Smetalnika

a guest
Apr 5th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. using System;
  2.  
  3. class Smetalnik
  4. {
  5.  
  6.     static void MoveLeft(uint[] nums, int width, int position, int line)
  7.     {
  8.         //adjust the position
  9.         if (position >= width)
  10.         {
  11.             position = 0;
  12.         }
  13.         else if (position < 0)
  14.         {
  15.             position = width - 1;
  16.         }
  17.         else
  18.         {
  19.             position = width - 1 - position;
  20.         }
  21.  
  22.         int bitsCounter = 0;
  23.         for (int bitPosition = position; bitPosition < width; bitPosition++)
  24.         {
  25.             //count bits
  26.             if ((nums[line] & (1 << bitPosition)) != 0)
  27.             {
  28.                 bitsCounter++;
  29.  
  30.                 //reset bits 1 to 0
  31.                 uint mask = ~((uint)1 << bitPosition);
  32.                 nums[line] &= mask;
  33.             }
  34.         }
  35.  
  36.         //set new bits to 1 to the left
  37.         //int leftMostPosition = width - 1;
  38.         for (int i = 0; i < bitsCounter; i++)
  39.         {
  40.             nums[line] |= (uint)(1 << width - 1 - i);
  41.         }
  42.     }
  43.  
  44.     static void Main()
  45.     {
  46.         int width = int.Parse(Console.ReadLine());
  47.         uint[] nums = new uint[8];
  48.         for (int i = 0; i < nums.Length; i++)
  49.         {
  50.             nums[i] = uint.Parse(Console.ReadLine());
  51.         }
  52.  
  53.  
  54.         while (true)
  55.         {
  56.             string cmd = Console.ReadLine();
  57.             if (cmd == "stop")
  58.             {
  59.                 ulong emptyCols = (ulong)CountEmptyCols(nums, width);
  60.                 ulong sum = 0;
  61.                 for (int i = 0; i < nums.Length; i++)
  62.                 {
  63.                     sum += nums[i];
  64.                 }
  65.                 ulong result = sum * emptyCols;
  66.                 Console.WriteLine(result);
  67.                 return;
  68.             }
  69.             else if (cmd == "left")
  70.             {
  71.                 int line = int.Parse(Console.ReadLine());
  72.                 int position = int.Parse(Console.ReadLine());
  73.                 MoveLeft(nums, width, position, line);
  74.             }
  75.             else if (cmd == "right")
  76.             {
  77.                 int line = int.Parse(Console.ReadLine());
  78.                 int position = int.Parse(Console.ReadLine());
  79.                 MoveRight(nums, width, line, position);
  80.             }
  81.             else if (cmd == "reset")
  82.             {
  83.                 for (int line = 0; line < 8; line++)
  84.                 {
  85.                     MoveLeft(nums, width, width - 1, line);
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     private static int CountEmptyCols(uint[] nums, int width)
  92.     {
  93.         int emptyCounter = 0;
  94.         for (int col = 0; col < width; col++)
  95.         {
  96.             bool isEmpty = true;
  97.             for (int line = 0; line < 8; line++)
  98.             {
  99.                 if ((nums[line] & (uint)(1 << col)) != 0)
  100.                 {
  101.                     isEmpty = false;
  102.                     break;
  103.                 }
  104.             }
  105.             if (isEmpty)
  106.             {
  107.                 emptyCounter++;
  108.             }
  109.         }
  110.         return emptyCounter;
  111.     }
  112.  
  113.     private static void MoveRight(uint[] nums, int width, int line, int position)
  114.     {
  115.         //adjust the position
  116.         if (position >= width)
  117.         {
  118.             position = 0;
  119.         }
  120.         else if (position < 0)
  121.         {
  122.             position = width - 1;
  123.         }
  124.         else
  125.         {
  126.             position = width - 1 - position;
  127.         }
  128.  
  129.         int bitsCounter = 0;
  130.         for (int bitPosition = position; bitPosition >= 0; bitPosition--)
  131.         {
  132.             //count 1 bits and reset them to 0
  133.             if ((nums[line] & (1 << bitPosition)) != 0)
  134.             {
  135.                 bitsCounter++;
  136.                 uint mask = ~((uint)1 << bitPosition);
  137.                 nums[line] &= mask;
  138.             }
  139.         }
  140.         //set bits to 1
  141.         //nums[line] |= CalcPow(2, bitsCounter) - 1;
  142.  
  143.         for (int i = 0; i < bitsCounter; i++)
  144.         {
  145.             nums[line] |= (uint)1 << i;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement