package com.fishercoder.solutions;

/**
 * 289. Game of Life
 *
 * According to the Wikipedia's article: "The Game of Life, also known simply as Life,
 * is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

 Given a board with m by n cells, each cell has an initial state live (1) or dead (0).
 Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the
 following four rules (taken from the above Wikipedia article):

 Any live cell with fewer than two live neighbors dies, as if caused by under-population.
 Any live cell with two or three live neighbors lives on to the next generation.
 Any live cell with more than three live neighbors dies, as if by over-population..
 Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
 Write a function to compute the next state (after one update) of the board given its current state.

 Follow up:
 Could you solve it in-place? Remember that the board needs to be updated at the same time:
 You cannot update some cells first and then use their updated values to update other cells.
 In this question, we represent the board using a 2D array.
 In principle, the board is infinite, which would cause problems when the active area encroaches the
 border of the array. How would you address these problems?*/

public class _289 {
    public void gameOfLife(int[][] board) {
        int height = board.length;
        int width = board[0].length;
        int[][] next = new int[height][width];

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int liveCellsCount = 0;
                //count all its live cells

                if (j + 1 < width && board[i][j + 1] == 1) {
                    liveCellsCount++;//right cell
                }
                if (j - 1 >= 0 && board[i][j - 1] == 1) {
                    liveCellsCount++;//left cell
                }
                if (i + 1 < height && board[i + 1][j] == 1) {
                    liveCellsCount++;//down cell
                }
                if (i - 1 >= 0 && board[i - 1][j] == 1) {
                    liveCellsCount++;//up cell
                }
                if (i - 1 >= 0 && j - 1 >= 0 && board[i - 1][j - 1] == 1) {
                    liveCellsCount++;//up left cell
                }
                if (i - 1 >= 0 && j + 1 < width && board[i - 1][j + 1] == 1) {
                    liveCellsCount++;//up right cell
                }
                if (i + 1 < height && j - 1 >= 0 && board[i + 1][j - 1] == 1) {
                    liveCellsCount++;//down left cell
                }
                if (i + 1 < height && j + 1 < width && board[i + 1][j + 1] == 1) {
                    liveCellsCount++;//down right cell
                }

                if (board[i][j] == 1) {
                    if (liveCellsCount > 3 || liveCellsCount < 2) {
                        next[i][j] = 0;
                    } else {
                        next[i][j] = 1;
                    }
                } else if (board[i][j] == 0) {
                    if (liveCellsCount == 3) {
                        next[i][j] = 1;
                    }
                }
            }
        }

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] = next[i][j];
            }
        }
    }
}

@@ -1532,16 +1532,117 @@
 %5Bwidth%5D;
+%0A        int%5B%5D%5B%5D directions = %7B%7B-1, -1%7D, %7B-1, 0%7D, %7B-1, 1%7D, %7B0, 1%7D, %7B1, 1%7D, %7B1, 0%7D, %7B1, -1%7D, %7B0, -1%7D%7D;
 %0A%0A      
@@ -1844,498 +1844,123 @@
     
-if (j + 1 %3C width && board%5Bi%5D%5Bj + 1%5D == 1) %7B%0A                    liveCellsCount++;//right cell%0A                %7D%0A                if (j - 1 %3E= 0 && board%5Bi%5D%5Bj - 1%5D == 1) %7B%0A                    liveCellsCount++;//left cell%0A                %7D%0A                if (i + 1 %3C height && board%5Bi + 1%5D%5Bj%5D == 1) %7B%0A                    liveCellsCount++;//down cell%0A                %7D%0A                if (i - 1 %3E= 0 && board%5Bi - 1%5D%5Bj%5D == 1) %7B%0A                    liveCellsCount++;//up cell
+for (int%5B%5D dir : directions) %7B%0A                    int x = i + dir%5B0%5D;%0A                    int y = j + dir%5B1%5D;
 %0A    
-            %7D%0A
     
@@ -1967,37 +1967,33 @@
             if (
-i - 1
+x
  %3E= 0 && j - 1 %3E
@@ -1981,29 +1981,25 @@
  (x %3E= 0 && 
-j - 1
+y
  %3E= 0 && boa
@@ -1999,145 +1999,23 @@
  && 
-board%5Bi - 1%5D%5Bj - 1%5D == 1) %7B%0A                    liveCellsCount++;//up left cell%0A                %7D%0A                if (i - 1 %3E= 0 && j + 1
+x %3C height && y
  %3C w
@@ -2032,20 +2032,12 @@
 ard%5B
-i - 1%5D%5Bj + 1
+x%5D%5By
 %5D ==
@@ -2058,147 +2058,8 @@
     
-        liveCellsCount++;//up right cell%0A                %7D%0A                if (i + 1 %3C height && j - 1 %3E= 0 && board%5Bi + 1%5D%5Bj - 1%5D == 1) %7B%0A
     
@@ -2062,32 +2062,24 @@
             
-        
 liveCellsCou
@@ -2087,24 +2087,8 @@
 t++;
-//down left cell
 %0A   
@@ -2092,38 +2092,32 @@
             
-    %7D%0A
         
         if (
@@ -2108,137 +2108,9 @@
     
-        if (i + 1 %3C height && j + 1 %3C width && board%5Bi + 1%5D%5Bj + 1%5D == 1) %7B%0A                    liveCellsCount++;//down right cell
+%7D
 %0A   
@@ -2208,14 +2208,15 @@
 unt 
-%3E 3 %7C%7C
+%3C= 3 &&
  liv
@@ -2231,84 +2231,16 @@
 unt 
-%3C
+%3E=
  2) %7B%0A
-                        next%5Bi%5D%5Bj%5D = 0;%0A                    %7D else %7B%0A
     

refactor 289