**Story**  
On some island lives a chameleon population. Chameleons here can be of only one of three colors - red, green and blue. Whenever two chameleons of different colors meet, they can change their colors to a third one (i.e. when red and blue chameleons meet, they can both become green). There is no other way for chameleons to change their color (in particular, when red and blue chameleons meet, they can't become both red, only third color can be assumed).  
Chameleons want to become of one certain color. They may plan they meetings to reach that goal. Chameleons want to know, how fast their goal can be achieved (if it can be achieved at all).

**Formal problem**  
*Input:*  
Color is coded as integer, 0 - red, 1 - green, 2 - blue.
Chameleon starting population is given as an array of three integers, with index corresponding to color (i.e. [2, 5, 3] means 2 red, 5 green and 3 blue chameleons). All numbers are non-negative, their sum is between `1` and `int.MaxValue` (maximal possible value for `int` type, in other languages). Desired color is given as an integer from 0 to 2.  
*Output:*  
`Kata.Chameleon` should return *minimal* number of meetings required to change all chameleons to a given color, or -1 if it is impossible (for example, if all chameleons are initially of one other color).  
  
**Notes and hints**  
-- Some tests use rather big input values. Be effective.  
-- There is a strict proof that answer is either -1 or no greater than total number of chameleons (thus, return type `int` is justified). Don't worry about overflow.  
  
**Credits**  
Kata based on "Chameleons" puzzle from braingames.ru: http://www.braingames.ru/?path=comments&puzzle=226 (russian, not translated).

Difficulty: introductory

Starter code:

def chameleon(chameleons, desiredColor):
	

Solutions:
def chameleon(chameleons, color):
    (_,a), (_,b), (_,c) = sorted((i==color, v) for i,v in enumerate(chameleons))
    return -1 if not a and not c or (b-a) % 3 else b

def chameleon(chameleons, desiredColor):
    
    color_count = dict(list(zip(['r', 'g', 'b'], chameleons)))
    target = ['r', 'g', 'b'][desiredColor]
    other_color = [i for i in ['r', 'g', 'b'] if i != target]
    to_meet =sorted([(k, color_count[k]) for k in other_color], key=lambda x: x[1])

    if to_meet[0][1] == to_meet[1][1]:
        return to_meet[0][1]
    meets = to_meet[0][1]
    meet_left = to_meet[1][1] - meets
    color_count[target] +=  to_meet[0][1]
    # print meet_left, meets
    if meet_left % 3 != 0 or color_count[target] == 0:

        return -1
    return meets + meet_left


chameleon=lambda C,d:(lambda c,a,b:-1if a==c<1or(b-a)%3else b)(*[C.pop(d)]+sorted(C))

chameleon=lambda C,d:(lambda a,b,c:-1if a==c<1or(b-a)%3else b)(*sorted([C[i]for i in(0,1,2)if i!=d])+[C[d]])

def chameleon(C,d):
    c=C.pop(d)
    a,b=sorted(C)
    return(a==c<1)+(b-a)%3and-1 or b

def chameleon(chameleons, desiredColor):
    r=chameleons[:]
    r.pop(desiredColor)
    if max(r)==min(r):
        return max(r)
    d=max(r)-min(r)
    dc=chameleons[desiredColor]+2*min(r)
    if d%3!=0 or d//3>dc:
        return -1
    return max(r)

def chameleon(chameleons, desiredColor):
    # rotate a, b, c as a: desiredColor and b ≥ c
    a, b, c = chameleons[desiredColor:] + chameleons[:desiredColor]
    if b < c:
        b, c = c, b
    # combine a and b to become c x times so that b - x = c + 2x i.e. 3x = b - c
    if (b - c) % 3 != 0 or a < (b - c) / 3:
        return -1
    # combine b and c to become a for (b - x) times or (c + 2x) times
    # total number of combinations = x + b - x = b
    return b

# chameleons is int[3], desiredColor is int from 0 to 2
def chameleon(chameleons, desiredColor):
    perm_count = 0
    if desiredColor == 0: cD, cA, cB = chameleons
    elif desiredColor == 1: cA, cD, cB = chameleons
    else: cA, cB, cD = chameleons
    
    while cA != 0 or cB != 0:
        if cA != 0 and cB != 0:
            d = min(cA, cB)
            cA, cB, cD = cA -d, cB -d, cD + 2 * d
        elif cA == 0:
            d = min(cB // 3, cD)
            cA, cB, cD = cA + 2 * d, cB - d, cD - d
        elif cB == 0:
            d = min(cA // 3, cD)
            cA, cB, cD = cA - d, cB + 2 * d, cD - d
        if d == 0: return -1
        perm_count += d
    return perm_count
        
        
    # Here be code


# chameleons is int[3], desiredColor is int from 0 to 2
def chameleon(chameleons, desiredColor):
    print(chameleons, desiredColor)
    other_coler = [j for i,j in  enumerate(chameleons) if i != desiredColor]
    
    #判断是否有解
    if sum([i==0 for i in chameleons]) == 2 and chameleons[desiredColor] == 0:
        return -1
    elif not (other_coler[0] - other_coler[1])%3 == 0:
        return -1
        
    #判断meet次数
    return max(other_coler)

def chameleon(chameleons, desiredColor):
    color = chameleons.pop(desiredColor)
    d,r = divmod(abs(chameleons[0]-chameleons[1]),3)
    if d < color and r == 0:
        return min(chameleons) + 3 * d
    else:
        return -1