func gameOfLife(board [][]int) { m, n := len(board), len(board[0]) xDir := []int{1, 1, 0, -1, -1, -1, 0, 1} yDir := []int{0, -1, -1, -1, 0, 1, 1, 1} for y := 0; y < m; y++ { for x := 0; x < n; x++ { cnt := 0 for i := 0; i < 8; i++ { xPos, yPos := x+xDir[i], y+yDir[i] if 0 <= xPos && xPos < n && 0 <= yPos && yPos < m { cnt += board[yPos][xPos] & 1 } } if cnt == 2 { // Keep current state board[y][x] |= (board[y][x] & 1) << 1 } else if cnt == 3 { // Live :-) board[y][x] |= 2 } // Else, die X-( } } for y := 0; y < m; y++ { for x := 0; x < n; x++ { board[y][x] >>= 1 } } }