Advertisement
piotrek77

anth

Oct 7th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. namespace LightBulbs
  2. {
  3.     public class LightBulbs : ILightBulbs
  4.     {
  5.         public new static ILightBulbs GetInstance()
  6.         {
  7.             return lightBulbsInstance ?? (lightBulbsInstance = new LightBulbs());
  8.         }
  9.  
  10.         public override int CountLightsOn(bool[,] lightsBoard, int s)
  11.         {
  12.             int rows = lightsBoard.GetLength(0);
  13.             int cols = lightsBoard.GetLength(1);
  14.  
  15.             for (int i = 0; i < s; ++i)
  16.             {
  17.                 var copy = this.Copy(lightsBoard);
  18.  
  19.                 for (int r = 0; r < rows; ++r)
  20.                 {
  21.                     for (int c = 0; c < cols; ++c)
  22.                     {
  23.                         if (this.IsCorner(copy, r, c))
  24.                             continue;
  25.  
  26.                         int turnedOnNeighbors = this.FindTurnedOnNeighbors(copy, r, c);
  27.  
  28.                         if (copy[r, c])
  29.                         {
  30.                             if (!(turnedOnNeighbors == 2 || turnedOnNeighbors == 3))
  31.                                 lightsBoard[r, c] = false;
  32.                         }
  33.                         else
  34.                         {
  35.                             if (turnedOnNeighbors == 3)
  36.                                 lightsBoard[r, c] = true;
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.  
  42.             int count = 0;
  43.  
  44.             for (int r = 0; r < rows; ++r)
  45.             {
  46.                 for (int c = 0; c < cols; ++c)
  47.                 {
  48.                     if (lightsBoard[r, c])
  49.                         count++;
  50.                 }
  51.             }
  52.  
  53.             return count;
  54.         }
  55.  
  56.         private bool[,] Copy(bool[,] from)
  57.         {
  58.             int rows = from.GetLength(0);
  59.             int cols = from.GetLength(1);
  60.  
  61.             bool[,] copy = new bool[rows, cols];
  62.             for (int r = 0; r < rows; ++r)
  63.                 for (int c = 0; c < cols; ++c)
  64.                     copy[r, c] = from[r, c];
  65.  
  66.             return copy;
  67.         }
  68.  
  69.         private bool IsCorner(bool[,] lightsBoard, int row, int col)
  70.         {
  71.             int rows = lightsBoard.GetLength(0);
  72.             int cols = lightsBoard.GetLength(1);
  73.  
  74.             if (row == 0 && col == 0 ||
  75.                 row == 0 && col == cols - 1 ||
  76.                 row == rows - 1 && col == 0 ||
  77.                 row == rows - 1 && col == cols - 1)
  78.             {
  79.                 return true;
  80.             }
  81.  
  82.             return false;
  83.         }
  84.  
  85.         private int FindTurnedOnNeighbors(bool[,] lightsBoard, int row, int col)
  86.         {
  87.             int count = 0;
  88.  
  89.             int rows = lightsBoard.GetLength(0);
  90.             int cols = lightsBoard.GetLength(1);
  91.  
  92.            
  93.             for (int r = -1; r <= 1; ++r)
  94.             {
  95.                 for (int c = -1; c <= 1; ++c)
  96.                 {
  97.                     int nRow = row + r;
  98.                     int nCol = col + c;
  99.  
  100.                     if (nRow == row && nCol == col)
  101.                         continue;
  102.  
  103.                     if (nRow >= 0 && nCol >= 0 && nRow <= rows - 1 && nCol <= cols - 1)
  104.                     {
  105.                         if (lightsBoard[nRow, nCol])
  106.                             count++;
  107.                     }
  108.                 }
  109.             }
  110.  
  111.             return count;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement