Advertisement
fbinnzhivko

05.00 Bit Shooter

Apr 21st, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.15 KB | None | 0 0
  1. /* Problem 5 – Bit Shooter
  2. We are given a bit field in the form of 64-bit integer number. We shoot it 3 times. Each shoot has a center and a size.
  3.  * The shoot damages size bits around the shoot center (makes these bits 0). Finally, the bit field is split into left side (bits 63 … 32)
  4.  * and right side (bits 31 … 0). Write a program that calculates how many bits survive (have value 1)
  5.  * after the shoots in the left side and in the right side of the bit field. The bits are numbered as traditionally in programming:
  6.  * from right to left from 0 to 63.
  7. Input
  8. The input data should be read from the console. It will consist of exactly 4 lines:
  9. • At the first line you will have a 64-bit integer, corresponding to the bit field.
  10. • At each of the next 3 lines we have 2 numbers: shoot center and shoot size – integers, split by a space.
  11. The input data will always be valid and in the format described. There is no need to check it explicitly.
  12. Output
  13. The output should be printed on the console. It should consists of exactly 2 lines:
  14. • The first line print "left: …" and the number of alive bits in the left side.
  15. • The second line print "right: …" and the number of alive bits in the right side.
  16. Constraints
  17. • The bit field will be a 64-bit integer in the range [0 … 18 446 744 073 709 551 615].
  18. • The values for the center will be integers will be integers in range [0 … 63].
  19. • The values for the size will be odd integers in range [1 … 99].
  20.  */
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. class BitShooter
  25. {
  26.     static void Main()
  27.     {
  28.         // input
  29.         ulong bitField = ulong.Parse(Console.ReadLine());
  30.  
  31.         string binary = Convert.ToString((long)bitField, 2).PadLeft(64, '0');
  32.  
  33.         List<int[]> targets = new List<int[]>();
  34.  
  35.         for (int i = 0; i < 3; i++)
  36.         {
  37.             targets.Add(Console.ReadLine().Split(' ').Select(int.Parse).ToArray());
  38.         }
  39.  
  40.         // preparing the binary masks
  41.         string number1B = new string('1', targets[0][1]);
  42.         string number2B = new string('1', targets[1][1]);
  43.         string number3B = new string('1', targets[2][1]);
  44.  
  45.         // in case you want to see the binary representations of the masks
  46.         //Console.WriteLine(number1B);
  47.         //Console.WriteLine(number2B);
  48.         //Console.WriteLine(number3B);
  49.  
  50.         // converting them into decimal numbers
  51.         ulong number1 = BinaryToDecimal(number1B);
  52.         ulong number2 = BinaryToDecimal(number2B);
  53.         ulong number3 = BinaryToDecimal(number3B);
  54.  
  55.         // in case you want to see the the mask calculation numbers
  56.         //Console.WriteLine(number1);
  57.         //Console.WriteLine(number2);
  58.         //Console.WriteLine(number3);
  59.  
  60.         // calculating the shifting of the masks to their respective centers
  61.         int shift1 = targets[0][0] - number1B.Length / 2;
  62.         int shift2 = targets[1][0] - number2B.Length / 2;
  63.         int shift3 = targets[2][0] - number3B.Length / 2;
  64.  
  65.         // shifting the masks
  66.         // taking care not to shift the masks in a wrong direction
  67.         ulong mask1 = 0;
  68.         ulong mask2 = 0;
  69.         ulong mask3 = 0;
  70.         if (shift1 > 0)
  71.         {
  72.             mask1 = number1 << shift1;
  73.         }
  74.         else
  75.         {
  76.             mask1 = number1 >> Math.Abs(shift1);
  77.         }
  78.  
  79.         if (shift2 > 0)
  80.         {
  81.             mask2 = number2 << shift2;
  82.         }
  83.         else
  84.         {
  85.             mask2 = number2 >> Math.Abs(shift2);
  86.         }
  87.         if (shift3 > 0)
  88.         {
  89.             mask3 = number3 << shift3;
  90.         }
  91.         else
  92.         {
  93.             mask3 = number3 >> Math.Abs(shift3);
  94.         }
  95.  
  96.         // in case you want to see the already shifted masks
  97.         //Console.WriteLine(binary);
  98.         //Console.WriteLine(Convert.ToString((long)mask1, 2).PadLeft(64, '0'));
  99.         //Console.WriteLine(Convert.ToString((long)mask2, 2).PadLeft(64, '0'));
  100.         //Console.WriteLine(Convert.ToString((long)mask3, 2).PadLeft(64, '0'));
  101.  
  102.         // applying the masks
  103.         bitField = bitField | mask1;
  104.         bitField = bitField ^ mask1;
  105.  
  106.         bitField = bitField | mask2;
  107.         bitField = bitField ^ mask2;
  108.  
  109.         bitField = bitField | mask3;
  110.         bitField = bitField ^ mask3;
  111.  
  112.         // converting the bitfield number into a already shooted at binary string
  113.         binary = Convert.ToString((long)bitField, 2).PadLeft(64, '0');
  114.  
  115.         // in case you want to see the binary representation of the bitfield after all the 3 shootings
  116.         //Console.WriteLine(binary);
  117.  
  118.         // dividing the binary bitfield string into a left and a right part
  119.         string left = binary.Substring(0, 32);
  120.         string right = binary.Substring(32);
  121.  
  122.         // in case you want to see the left and right parts of the bitfield
  123.         //Console.WriteLine(left);
  124.         //Console.WriteLine(right);
  125.  
  126.         // converting the left and right strings into decimal numbers
  127.         // we use bigger data type in order to prevent negative decimal numbers
  128.         long leftNum = Convert.ToInt64(left, 2);
  129.         long rightNum = Convert.ToInt64(right, 2);
  130.  
  131.         // counting the bits of the decimal numbers
  132.         int counterL = 0;
  133.         int counterR = 0;
  134.         long temp = 0;
  135.         do
  136.         {
  137.             temp = leftNum & 1;
  138.             if (temp == 1)
  139.             {
  140.                 counterL++;
  141.             }
  142.             leftNum = leftNum >> 1;
  143.         } while (leftNum != 0);
  144.  
  145.         do
  146.         {
  147.             temp = rightNum & 1;
  148.             if (temp == 1)
  149.             {
  150.                 counterR++;
  151.             }
  152.             rightNum = rightNum >> 1;
  153.         } while (rightNum != 0);
  154.  
  155.         // printing the results
  156.         Console.WriteLine("left: {0}", counterL);
  157.         Console.WriteLine("right: {0}", counterR);
  158.     }
  159.  
  160.     private static ulong BinaryToDecimal(string number)
  161.     {
  162.         ulong decNumber = 0;
  163.         int index = 0;
  164.         for (int i = number.Length - 1; i >= 0; i--)
  165.         {
  166.             decNumber += (ulong)(int.Parse(number[i].ToString()) * Math.Pow(2, index));
  167.             index++;
  168.         }
  169.         return decNumber;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement