Advertisement
g-stoyanov

Telerik Warhead

Mar 19th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Warhead
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[,] matrix = new int[16, 16];
  11.             for (int i = 0; i < 16; i++)
  12.             {
  13.                 string input = Console.ReadLine();
  14.                 char[] lineChars = input.ToCharArray();
  15.                 for (int ch = 0; ch < 16; ch++)
  16.                 {
  17.                     matrix[i, ch] = int.Parse(lineChars[ch].ToString());
  18.                 }
  19.             }
  20.  
  21.             bool isArmed = true;
  22.  
  23.             while (isArmed)
  24.             {
  25.                 string input = Console.ReadLine();
  26.                 switch (input)
  27.                 {
  28.                     case "hover":
  29.                         {
  30.                             int line = int.Parse(Console.ReadLine());
  31.                             int cell = int.Parse(Console.ReadLine());
  32.                             Console.WriteLine(Hover(line, cell, matrix));
  33.                         }
  34.                         break;
  35.  
  36.                     case "operate":
  37.                         {
  38.                             int line = int.Parse(Console.ReadLine());
  39.                             int cell = int.Parse(Console.ReadLine());
  40.                             string result = Operate(line, cell, matrix);
  41.                             if (result != string.Empty)
  42.                             {
  43.                                 isArmed = false;
  44.                                 Console.WriteLine(result);
  45.                             }
  46.                         }
  47.                         break;
  48.  
  49.                     case "cut":
  50.                         {
  51.                             isArmed = false;
  52.                             string wireColor = Console.ReadLine();
  53.                             if (wireColor == "left")
  54.                             {
  55.                                 wireColor = "red";
  56.                             }
  57.  
  58.                             if (wireColor == "right")
  59.                             {
  60.                                 wireColor = "blue";
  61.                             }
  62.  
  63.                             Console.WriteLine(CutWire(matrix, wireColor));
  64.                         }
  65.                         break;
  66.                 }
  67.             }
  68.         }
  69.  
  70.         private static string Hover(int line, int cell, int[,] matrix)
  71.         {
  72.             string result = "-";
  73.             if (matrix[line, cell] == 1)
  74.             {
  75.                 result = "*";
  76.             }
  77.  
  78.             return result;
  79.         }
  80.  
  81.         private static string Operate(int line, int cell, int[,] matrix)
  82.         {
  83.             string result = string.Empty;
  84.             if (matrix[line, cell] == 1)
  85.             {
  86.                 StringBuilder builder = new StringBuilder();
  87.                 builder.AppendLine("missed");
  88.                 builder.AppendLine(CountCapacitors(matrix, 0, 15, 0, 15).ToString());
  89.                 builder.Append("BOOM");
  90.                 result = builder.ToString();
  91.             }
  92.             else if (CheckIsCapacitor(matrix, line, cell))
  93.             {
  94.                 matrix[line - 1, cell - 1] = 0;
  95.                 matrix[line - 1, cell] = 0;
  96.                 matrix[line - 1, cell + 1] = 0;
  97.                 matrix[line + 1, cell - 1] = 0;
  98.                 matrix[line + 1, cell] = 0;
  99.                 matrix[line + 1, cell + 1] = 0;
  100.                 matrix[line, cell - 1] = 0;
  101.                 matrix[line, cell + 1] = 0;
  102.             }
  103.  
  104.             return result;
  105.         }
  106.  
  107.         private static int CountCapacitors(int[,] matrix, int startLine,
  108.             int endLine, int startCell, int endCell)
  109.         {
  110.             int count = 0;
  111.             for (int line = startLine + 1; line < endLine; line++)
  112.             {
  113.                 for (int cell = startCell + 1; cell < endCell; cell++)
  114.                 {
  115.                     if (CheckIsCapacitor(matrix, line, cell))
  116.                     {
  117.                         count++;
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             return count;
  123.         }
  124.  
  125.         private static bool CheckIsCapacitor(int[,] matrix, int line, int cell)
  126.         {
  127.             try
  128.             {
  129.                 return matrix[line - 1, cell - 1] == 1 && matrix[line + 1, cell + 1] == 1 &&
  130.                      matrix[line - 1, cell] == 1 && matrix[line + 1, cell] == 1 &&
  131.                      matrix[line - 1, cell + 1] == 1 && matrix[line + 1, cell - 1] == 1 &&
  132.                      matrix[line, cell - 1] == 1 && matrix[line, cell + 1] == 1;
  133.             }
  134.             catch
  135.             {
  136.                 return false;
  137.             }
  138.         }
  139.  
  140.         private static string CutWire(int[,] matrix, string wireColor)
  141.         {
  142.             StringBuilder builder = new StringBuilder();
  143.             int redCapacitors = CountCapacitors(matrix, 0, 15, 0, 8);
  144.             int blueCapacitors = CountCapacitors(matrix, 0, 15, 7, 15);
  145.             switch (wireColor)
  146.             {
  147.                 case "red":
  148.                     {
  149.                         if (redCapacitors != 0)
  150.                         {
  151.                             builder.AppendLine(redCapacitors.ToString());
  152.                             builder.Append("BOOM");
  153.                         }
  154.                         else
  155.                         {
  156.                             builder.AppendLine(blueCapacitors.ToString());
  157.                             builder.Append("disarmed");
  158.                         }
  159.                     }
  160.                     break;
  161.  
  162.                 case "blue":
  163.                     {
  164.                         if (blueCapacitors != 0)
  165.                         {
  166.                             builder.AppendLine(blueCapacitors.ToString());
  167.                             builder.Append("BOOM");
  168.                         }
  169.                         else
  170.                         {
  171.                             builder.AppendLine(redCapacitors.ToString());
  172.                             builder.Append("disarmed");
  173.                         }
  174.                     }
  175.                     break;
  176.             }
  177.  
  178.             return builder.ToString();
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement