Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class AngryBitsChallenge
- {
- static void Main()
- {
- Console.WriteLine(DateTime.Now);
- int bits = 0;
- int maxBits = 9;
- int maxResult = 0;
- int[] outputCols = new int[8];
- int[] columns = new int[8];
- int[] maskNumbers = {0, 1, 2, 4, 8, 16, 32, 64, 128,};
- int[,] matrix = new int[8, 16];
- int[,] pigMatrix = new int[8, 8];
- string input = "";
- for (byte i = 0; i < 8; i++)
- {
- input = Convert.ToString(int.Parse(Console.ReadLine()), 2).PadLeft(8, '0');
- for (int a = 0; a < 8; a++)
- {
- pigMatrix[i, a] = int.Parse(Convert.ToString(input[a]));
- }
- }
- for (int a = 0; a <= 8; a++, columns[0] = maskNumbers[a-1])
- {
- for (int b = 0; b <= 8; b++, columns[1] = maskNumbers[b-1])
- {
- for (int c = 0; c <= 8; c++, columns[2] = maskNumbers[c-1])
- {
- for (int d = 0; d <= 8; d++, columns[3] = maskNumbers[d-1])
- {
- for (int e = 0; e <= 8; e++, columns[4] = maskNumbers[e-1])
- {
- for (int f = 0; f <= 8; f++, columns[5] = maskNumbers[f-1])
- {
- for (int g = 0; g <= 8; g++, columns[6] = maskNumbers[g-1])
- {
- for (int h1 = 0; h1 <= 8; h1++, columns[7] = maskNumbers[h1-1])
- {
- for (int x = 0; x < 8; x++)
- {
- input = Convert.ToString(columns[x], 2).PadLeft(8, '0');
- for (int i = 0; i < 8; i++)
- {
- matrix[i, x] = int.Parse(Convert.ToString(input[i]));
- matrix[i, x + 8] = pigMatrix[i, x];
- }
- }
- bits = 0;
- for (int k = 0; k < 8; k++)
- {
- for (int l = 0; l < 8; l++)
- {
- bits = bits + matrix[k, l];
- }
- }
- //--------------------------------------------Begin of code from Angry Bits task
- int y = 0;
- bool hit = false;
- int flight = 0;
- int killedPigs = 0;
- int result = 0;
- int direction = -1;
- for (int i = 7; i >= 0; i--)
- {
- hit = false;
- for (int Y = 0; Y < 8; Y++)
- {
- if (matrix[Y, i] == 1)
- {
- matrix[Y, i] = 0;
- direction = -1;
- killedPigs = 0;
- flight = 0;
- int h = i;
- y = Y;
- while (h < 16)
- {
- if (y + direction > 7)
- {
- break;
- }
- flight++;
- if (y + direction > 7 || y + direction < 0)
- {
- direction = direction * -1;
- }
- h++;
- y = y + direction;
- if (y > 7 || y < 0 || h > 15 || h < 0)
- {
- continue;
- }
- if (h > 7)
- {
- if (matrix[y, h] == 1)
- {
- hit = true;
- for (int z = y - 1; z < y + 2; z++)
- {
- for (int k = h - 1; k < h + 2; k++)
- {
- if (z > 7 || z < 0 || k > 15 || k < 0)
- {
- continue;
- }
- if (matrix[z, k] == 1)
- {
- killedPigs++;
- matrix[z, k] = 0;
- }
- }
- }
- }
- }
- if (hit)
- {
- break;
- }
- }
- }
- if (hit)
- {
- result = result + (flight * killedPigs);
- break;
- }
- }
- }
- int check = 0;
- for (int i = 0; i < 8; i++)
- {
- for (int z = 0; z < 16; z++)
- {
- check = check + matrix[i, z];
- }
- }
- //-------------------------------------------end of code from Angry Bits task
- if (result == maxResult && bits < maxBits)
- {
- maxResult = result;
- maxBits = bits;
- for (int j = 0; j < 8; j++)
- {
- outputCols[j] = columns[j];
- }
- }
- if (result > maxResult)
- {
- maxResult = result;
- maxBits = bits;
- for (int j = 0; j < 8; j++)
- {
- outputCols[j] = columns[j];
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- Console.WriteLine(DateTime.Now);
- Console.WriteLine("{0} scores with {1} bits." , maxResult, maxBits);
- for (int x = 0; x < 8; x++)
- {
- input = Convert.ToString(outputCols[x], 2).PadLeft(8, '0');
- for (int i = 0; i < 8; i++)
- {
- matrix[i, x] = int.Parse(Convert.ToString(input[i]));
- matrix[i, x + 8] = pigMatrix[i, x];
- }
- }
- for (int i = 0; i < 8; i++)
- {
- for (int z = 0; z < 16; z++)
- {
- Console.Write(matrix[i, z] + " ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement