Advertisement
g-stoyanov

AngryBitsChallenge

Jan 1st, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.73 KB | None | 0 0
  1. using System;
  2.  
  3. class AngryBitsChallenge
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine(DateTime.Now);
  8.         int bits = 0;
  9.         int maxBits = 9;
  10.         int maxResult = 0;
  11.         int[] outputCols = new int[8];
  12.         int[] columns = new int[8];
  13.         int[] maskNumbers = {0, 1, 2, 4, 8, 16, 32, 64, 128,};
  14.         int[,] matrix = new int[8, 16];
  15.         int[,] pigMatrix = new int[8, 8];
  16.         string input = "";
  17.         for (byte i = 0; i < 8; i++)
  18.         {
  19.             input = Convert.ToString(int.Parse(Console.ReadLine()), 2).PadLeft(8, '0');
  20.             for (int a = 0; a < 8; a++)
  21.             {
  22.                 pigMatrix[i, a] = int.Parse(Convert.ToString(input[a]));
  23.             }
  24.         }
  25.         for (int a = 0; a <= 8; a++, columns[0] = maskNumbers[a-1])
  26.         {
  27.             for (int b = 0; b <= 8; b++, columns[1] = maskNumbers[b-1])
  28.             {
  29.                 for (int c = 0; c <= 8; c++, columns[2] = maskNumbers[c-1])
  30.                 {
  31.                     for (int d = 0; d <= 8; d++, columns[3] = maskNumbers[d-1])
  32.                     {
  33.                         for (int e = 0; e <= 8; e++, columns[4] = maskNumbers[e-1])
  34.                         {
  35.                             for (int f = 0; f <= 8; f++, columns[5] = maskNumbers[f-1])
  36.                             {
  37.                                 for (int g = 0; g <= 8; g++, columns[6] = maskNumbers[g-1])
  38.                                 {
  39.                                     for (int h1 = 0; h1 <= 8; h1++, columns[7] = maskNumbers[h1-1])
  40.                                     {
  41.                                         for (int x = 0; x < 8; x++)
  42.                                         {
  43.                                             input = Convert.ToString(columns[x], 2).PadLeft(8, '0');
  44.                                             for (int i = 0; i < 8; i++)
  45.                                             {
  46.                                                 matrix[i, x] = int.Parse(Convert.ToString(input[i]));
  47.                                                 matrix[i, x + 8] = pigMatrix[i, x];
  48.                                             }
  49.                                         }
  50.                                         bits = 0;
  51.                                         for (int k = 0; k < 8; k++)
  52.                                         {
  53.                                             for (int l = 0; l < 8; l++)
  54.                                             {
  55.                                                 bits = bits + matrix[k, l];
  56.                                             }
  57.                                         }
  58.                                         //--------------------------------------------Begin of code from Angry Bits task
  59.                                         int y = 0;
  60.                                         bool hit = false;
  61.                                         int flight = 0;
  62.                                         int killedPigs = 0;
  63.                                         int result = 0;
  64.                                         int direction = -1;
  65.  
  66.                                         for (int i = 7; i >= 0; i--)
  67.                                         {
  68.                                             hit = false;
  69.                                             for (int Y = 0; Y < 8; Y++)
  70.                                             {
  71.                                                 if (matrix[Y, i] == 1)
  72.                                                 {
  73.                                                     matrix[Y, i] = 0;
  74.                                                     direction = -1;
  75.                                                     killedPigs = 0;
  76.                                                     flight = 0;
  77.                                                     int h = i;
  78.                                                     y = Y;
  79.                                                     while (h < 16)
  80.                                                     {
  81.                                                         if (y + direction > 7)
  82.                                                         {
  83.                                                             break;
  84.                                                         }
  85.                                                         flight++;
  86.                                                         if (y + direction > 7 || y + direction < 0)
  87.                                                         {
  88.                                                             direction = direction * -1;
  89.                                                         }
  90.                                                         h++;
  91.                                                         y = y + direction;
  92.                                                         if (y > 7 || y < 0 || h > 15 || h < 0)
  93.                                                         {
  94.                                                             continue;
  95.                                                         }
  96.                                                         if (h > 7)
  97.                                                         {
  98.                                                             if (matrix[y, h] == 1)
  99.                                                             {
  100.                                                                 hit = true;
  101.                                                                 for (int z = y - 1; z < y + 2; z++)
  102.                                                                 {
  103.                                                                     for (int k = h - 1; k < h + 2; k++)
  104.                                                                     {
  105.                                                                         if (z > 7 || z < 0 || k > 15 || k < 0)
  106.                                                                         {
  107.                                                                             continue;
  108.                                                                         }
  109.                                                                         if (matrix[z, k] == 1)
  110.                                                                         {
  111.                                                                             killedPigs++;
  112.                                                                             matrix[z, k] = 0;
  113.                                                                         }
  114.                                                                     }
  115.                                                                 }
  116.                                                             }
  117.                                                         }
  118.                                                         if (hit)
  119.                                                         {
  120.                                                             break;
  121.                                                         }
  122.                                                     }
  123.                                                 }
  124.                                                 if (hit)
  125.                                                 {
  126.                                                     result = result + (flight * killedPigs);
  127.                                                     break;
  128.                                                 }
  129.                                             }
  130.                                         }
  131.                                         int check = 0;
  132.                                         for (int i = 0; i < 8; i++)
  133.                                         {
  134.                                             for (int z = 0; z < 16; z++)
  135.                                             {
  136.                                                 check = check + matrix[i, z];
  137.                                             }
  138.                                         }
  139.                                         //-------------------------------------------end of code from Angry Bits task
  140.                                         if (result == maxResult && bits < maxBits)
  141.                                         {
  142.                                             maxResult = result;
  143.                                             maxBits = bits;
  144.                                             for (int j = 0; j < 8; j++)
  145.                                             {
  146.                                                 outputCols[j] = columns[j];
  147.                                             }
  148.                                         }
  149.                                         if (result > maxResult)
  150.                                         {
  151.                                             maxResult = result;
  152.                                             maxBits = bits;
  153.                                             for (int j = 0; j < 8; j++)
  154.                                             {
  155.                                                 outputCols[j] = columns[j];
  156.                                             }
  157.                                         }
  158.                                     }
  159.                                 }
  160.                             }
  161.                         }
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.         Console.WriteLine(DateTime.Now);
  167.         Console.WriteLine("{0} scores with {1} bits." , maxResult, maxBits);
  168.         for (int x = 0; x < 8; x++)
  169.         {
  170.             input = Convert.ToString(outputCols[x], 2).PadLeft(8, '0');
  171.             for (int i = 0; i < 8; i++)
  172.             {
  173.                 matrix[i, x] = int.Parse(Convert.ToString(input[i]));
  174.                 matrix[i, x + 8] = pigMatrix[i, x];
  175.             }
  176.         }
  177.         for (int i = 0; i < 8; i++)
  178.         {
  179.             for (int z = 0; z < 16; z++)
  180.             {
  181.                 Console.Write(matrix[i, z] + "  ");
  182.             }
  183.             Console.WriteLine();
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement