Advertisement
Filkolev

Bit Paths

Jul 25th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 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.  
  7.  
  8. class BitPaths
  9. {
  10.     static void Main()
  11.     {
  12.         int N = int.Parse(Console.ReadLine());
  13.  
  14.         int[] numbers = new int[8];
  15.  
  16.         for (int i = 0; i < N; i++)
  17.         {
  18.             string currentInput = Console.ReadLine();
  19.  
  20.             int[] currentPath = Array.ConvertAll(currentInput.Split(','), x => int.Parse(x));
  21.  
  22.             int currentPosition = 3 - currentPath[0];
  23.  
  24.             for (int j = 0; j < 8; j++)
  25.             {
  26.                 int currentBit = (numbers[j] >> currentPosition) & 1;
  27.  
  28.                 if(currentBit == 1)
  29.                 {
  30.                     int mask = ~(1 << currentPosition);
  31.                     numbers[j] = numbers[j] & mask;
  32.                 }
  33.                 else
  34.                 {
  35.                     int mask = 1 << currentPosition;
  36.                     numbers[j] = numbers[j] | mask;
  37.                 }
  38.  
  39.                 if (j == 7)
  40.                 {
  41.                     break;
  42.                 }
  43.  
  44.                 currentPosition -= currentPath[j + 1];
  45.             }
  46.         }
  47.  
  48.         int sum = numbers.Sum();
  49.  
  50.         Console.WriteLine(Convert.ToString(sum, 2));
  51.         Console.WriteLine(Convert.ToString(sum, 16).ToUpper());
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement