If you finish this kata, you can try [Insane Coloured Triangles](http://www.codewars.com/kata/insane-coloured-triangles) by Bubbler, which is a ***much*** harder version of this one.

A coloured triangle is created from a row of colours, each of which is red, green or blue. Successive rows, each containing one fewer colour than the last, are generated by considering the two touching colours in the previous row. If these colours are identical, the same colour is used in the new row. If they are different, the missing colour is used in the new row. This is continued until the final row, with only a single colour, is generated.

The different possibilities are:
```
Colour here:        G G        B G        R G        B R
Becomes colour:      G          R          B          G
```

With a bigger example:
```
R R G B R G B B
 R B R G B R B
  G G B R G G
   G R G B G
    B B R R
     B G R
      R B
       G
```
  
You will be given the first row of the triangle as a string and its your job to return the final colour which would appear in the bottom row as a string. In the case of the example above, you would the given `RRGBRGBB` you should return `G`.

* The input string will only contain the uppercase letters `R, G, B` and there will be at least one letter so you do not have to test for invalid input.
* If you are only given one colour as the input, return that colour. 


*Adapted from the 2017 British Informatics Olympiad*

Source: codewars | Difficulty: EASY
Tags: String algorithms

Starter code: def triangle(row):
	

Solutions:
COLORS = set('RGB')

def triangle(row):
	while len(row) > 1:
		row = ''.join((a if a == b else (COLORS - {a, b}).pop() for (a, b) in zip(row, row[1:])))
	return row


def triangle(row):
	dicts = {'GG': 'G', 'BB': 'B', 'RR': 'R', 'BR': 'G', 'BG': 'R', 'GB': 'R', 'GR': 'B', 'RG': 'B', 'RB': 'G'}
	if len(row) > 2:
		s = ''
		for i in range(len(row) - 1):
			s = s + dicts[row[i:i + 2]]
		row = s
		return triangle(row)
	elif len(row) > 1:
		return dicts[row]
	else:
		return row


def triangle(row):
	while len(row) > 1:
		row = ''.join((({'R', 'G', 'B'} - {a, b}).pop() if a != b else a for (a, b) in zip(row, row[1:])))
	return row


comb = {'GG': 'G', 'BR': 'G', 'RB': 'G', 'RR': 'R', 'BG': 'R', 'GB': 'R', 'BB': 'B', 'RG': 'B', 'GR': 'B'}

def triangle(row):
	if len(row) == 1:
		return row
	return triangle(''.join((comb[row[i:i + 2]] for i in range(len(row) - 1))))


colors = set('RGB')

def triangle(row):
	while len(row) > 1:
		row = [a if a == b else colors.difference(a).difference(b).pop() for (a, b) in zip(row, row[1:])]
	return row[0]


def rgb(n, m):
	if n == m:
		return m
	else:
		return 'RGB'.replace(n, '').replace(m, '')

def triangle(row):
	temp = ''
	ans = row
	while len(ans) != 1:
		for n in range(len(ans) - 1):
			temp += rgb(ans[n], ans[n + 1])
		ans = temp
		temp = ''
	return ans


def triangle(row):
	if len(row) == 1:
		return row[0]
	colors = set(['B', 'G', 'R'])
	newrow = []
	for (l, r) in zip(row, row[1:]):
		if l == r:
			newrow.append(l)
		else:
			newrow.extend(colors - set([l, r]))
	return triangle(newrow)


def triangle(row):
	color = {'GG': 'G', 'BB': 'B', 'RR': 'R', 'BG': 'R', 'GR': 'B', 'BR': 'G', 'GB': 'R', 'RG': 'B', 'RB': 'G'}
	return triangle(''.join((color.get(l + row[j + 1]) for (j, l) in enumerate(row[:-1])))) if len(row) > 1 else row


def triangle(s):
	d = {'R': {'R': 'R', 'G': 'B', 'B': 'G'}, 'G': {'R': 'B', 'G': 'G', 'B': 'R'}, 'B': {'R': 'G', 'G': 'R', 'B': 'B'}}
	for i in range(len(s) - 1, 0, -1):
		s = [d[s[j]][s[j + 1]] for j in range(i)]
	return s[0]


def triangle(row):
	vector = list(row)
	while len(vector) > 1:
		vector = [((set('RGB') - {a, b}).pop(), a)[a == b] for (a, b) in zip(vector, vector[1:])]
	return vector[0]


def triangle(row):
	row = list(row)
	while len(row) > 1:
		row = [l if l == r else list(set('RGB') - set(l + r))[0] for (l, r) in zip(row, row[1:])]
	return row[0]


def mod3(n):
	c = 0
	while n % 3 == 0:
		n /= 3
		c += 1
	return (int(n % 3), c)

def triangle(row):
	rgb = {'R': 0, 'G': 1, 'B': 2}
	RGB = 'RGB'
	n = len(row)
	(x, y) = (1, 0)
	res = rgb[row[0]]
	for i in range(1, n):
		(xx, yy) = mod3(n - i)
		x = x * xx % 3
		y += yy
		(xx, yy) = mod3(i)
		x = x * xx % 3
		y -= yy
		if y == 0:
			res += rgb[row[i]] * x
	res %= 3
	if n % 2 == 0:
		res = (3 - res) % 3
	return RGB[res]


def possibilities(a, b):
	c = abs(ord(a) - ord(b))
	if c == 0:
		return a
	if c == 5:
		return 'R'
	if c == 11:
		return 'B'
	if c == 16:
		return 'G'
	return None

def triangle(row):
	while len(row) > 1:
		temp = ''
		for i in range(len(row) - 1):
			temp += possibilities(row[i], row[i + 1])
		row = temp
	return row[0]


def triangle(row):
	d = {'R': 0, 'G': 1, 'B': 2}
	a = [d.get(_) for _ in row]
	while len(a) > 1:
		a = list(map(lambda _: -(_[0] + _[1]), zip(a, a[1:])))
	return list(d.keys())[a[0] % 3]


def triangle(row):
	new_step = ''
	if len(row) == 1:
		return row
	while len(row) > 1:
		for i in range(1, len(row)):
			colors = ['R', 'G', 'B']
			if row[i - 1] == row[i]:
				new_step += row[i]
			else:
				colors.remove(row[i - 1])
				colors.remove(row[i])
				new_step += colors[0]
		if len(new_step) == 1:
			return new_step
		return triangle(new_step)


def triangle(row):
	while len(row) > 1:
		row = [[({*'RGB'} - {a, b}).pop(), a][a == b] for (a, b) in zip(row, row[1:])]
	return row[0]


def triangle(row):
	colors = ['R', 'G', 'B']
	while len(row) > 1:
		i = 0
		new_row = ''
		while i < len(row) - 1:
			if row[i] == row[i + 1]:
				new_row += row[i]
			else:
				for j in colors:
					if j != row[i] and j != row[i + 1]:
						new_row += j
			i += 1
		print(new_row)
		row = new_row
	return row


def triangle(s):
	n = len(s)
	out = []
	if n == 1:
		return s
	else:
		for i in range(n - 1):
			f = ['R', 'G', 'B']
			if s[i] == s[i + 1]:
				out.append(s[i])
			else:
				f.remove(str(s[i]))
				f.remove(str(s[i + 1]))
				out.extend(f)
		s = ''.join(out)
	if len(s) == 1:
		return s
	else:
		return triangle(s)


triangle = t = lambda r: len(r) > 1 and t(''.join((a if a == b else (set('RGB') - {a, b}).pop() for (a, b) in zip(r, r[1:])))) or r


COLS = {('G', 'G'): 'G', ('B', 'B'): 'B', ('R', 'R'): 'R', ('G', 'B'): 'R', ('G', 'R'): 'B', ('R', 'B'): 'G', ('R', 'G'): 'B', ('B', 'R'): 'G', ('B', 'G'): 'R'}

def triangle(row):
	while len(row) > 1:
		row = [COLS[x, y] for (x, y) in zip(row[:-1], row[1:])]
	return row[0]


t = triangle = lambda r: r[0] if len(r) == 1 else t([a if a == b else (set('RGB') - {a, b}).pop() for (a, b) in zip(r, r[1:])])


def triangle(row):
	COMB_I = [[0, 2, 1], [2, 1, 0], [1, 0, 2]]
	IDX = {'R': 0, 'G': 1, 'B': 2}
	IDX_R = {0: 'R', 1: 'G', 2: 'B'}
	ri = [IDX[r] for r in row]
	for k in range(len(row) - 1):
		ri = [COMB_I[i][j] for (i, j) in zip(ri[:-1], ri[1:])]
	return IDX_R[ri[0]]


def triangle(row):
	l = row
	while len(l) > 1:
		temp = ''
		for i in range(len(l) - 1):
			if l[i] == 'R' and l[i + 1] == 'G':
				col = 'B'
			elif l[i] == 'G' and l[i + 1] == 'R':
				col = 'B'
			elif l[i] == 'G' and l[i + 1] == 'B':
				col = 'R'
			elif l[i] == 'B' and l[i + 1] == 'G':
				col = 'R'
			elif l[i] == 'B' and l[i + 1] == 'R':
				col = 'G'
			elif l[i] == 'R' and l[i + 1] == 'B':
				col = 'G'
			elif l[i] == l[i + 1]:
				col = l[i]
			temp = temp + col
		l = temp
	return l


def triangle(row):

	def add(c1, c2):
		return c1 if c1 == c2 else 'RGB'.replace(c1, '').replace(c2, '')
	while len(row) > 1:
		s = ''
		for i in range(0, len(row) - 1):
			s += add(row[i], row[i + 1])
		row = s
	return row


def triangle(a):
	q = len(a)
	if q == 1:
		return a
	b = 1
	g = 2
	r = 3
	a = a.replace('B', '1')
	a = a.replace('G', '2')
	a = a.replace('R', '3')
	v = int(a)
	kj = 1
	me = 0
	while me == 0:
		if q > 3 ** kj:
			kj = kj + 1
		else:
			me = 1
	g = 3 ** (kj - 1)
	count = 0
	while q != 1:
		if q > g:
			for w1 in range(0, q - g):
				q1 = str(v)
				e = 3 - (int(q1[w1]) + int(q1[w1 + g])) % 3
				count = count + e * 10 ** (q - w1 - g - 1)
			print((g, q))
			q = q - g
			if q > g:
				g = g
			else:
				g = int(g / 3)
			print((g, q))
			v = count
			print(count)
		else:
			for i in range(1, q):
				e = 3 - (v // 10 ** (q - i) - 10 * (v // 10 ** (q - i + 1)) + v // 10 ** (q - i - 1) - 10 * (v // 10 ** (q - i))) % 3
				count = count + e * 10 ** (q - i - 1)
			q = q - 1
			v = count
			print(count)
		if q == 1:
			if count == 1:
				return 'B'
			elif count == 2:
				return 'G'
			else:
				return 'R'
		count = 0


def triangle(Notorious_RBG):
	if len(Notorious_RBG) == 1:
		return Notorious_RBG
	The_Notorious = 'RBG'
	The = [Notorious_RBG[x:x + 2] for x in range(len(Notorious_RBG) - 1)]
	RBG = ''.join([x[0] if x[0] == x[1] else The_Notorious.strip(x) for x in The])
	if len(RBG) == 1:
		return RBG
	else:
		return triangle(RBG)


def triangle(row):
	k = list(row)
	comb = {'R', 'G', 'B'}
	if len(k) == 1:
		return k[0]
	z = [k[i:i + 2] for i in range(len(k) - 1)]
	cont = []
	for x in z:
		diff = comb.difference(set(x))
		if len(diff) == 1:
			cont.append(diff.pop())
		else:
			cont.append(x[0])
	return triangle(''.join(cont))


def triangle(array):
	n = len(array)
	colors = {'R': 0, 'G': 1, 'B': 2}
	digit = {0: 'R', 1: 'G', 2: 'B'}
	a = [colors.get(item, item) for item in list(array)]

	def pascalRowFast(rowIndex):
		row = [0] * (rowIndex + 1)
		row[0] = row[-1] = 1
		for i in range(0, rowIndex >> 1):
			x = row[i] * (rowIndex - i) // (i + 1)
			row[i + 1] = row[rowIndex - 1 - i] = x
		return row
	total = sum([a * b for (a, b) in zip(pascalRowFast(n - 1), a)]) * (-1) ** (n - 1) % 3
	return digit[total]


def triangle(row):
	return row[0] if len(row) == 1 else triangle(['BGR'.replace(l1, '').replace(l2, '') if l1 != l2 else l1 for (l1, l2) in zip(row, row[1:])])


def triangle(row):
	while len(row) > 1:
		temp = ''
		for i in range(len(row) - 1):
			if row[i] == row[i + 1]:
				col = row[i]
			else:
				col = 'RGB'.replace(row[i], '').replace(row[i + 1], '')
			temp = temp + col
		row = temp
	return row


def triangle(row):
	if len(row) == 1:
		return row
	else:
		new_row = ''
		for (a, b) in zip(row[:-1], row[1:]):
			if a == b:
				new_row += a
			else:
				for letter in 'RGB':
					if letter not in (a, b):
						new_row += letter
						break
		return triangle(new_row)


def get(c1, c2):
	if c1 > c2:
		t = c1
		c1 = c2
		c2 = t
	if c1 == c2:
		return c1
	if c1 == 'B':
		if c2 == 'G':
			return 'R'
		else:
			return 'G'
	if c1 == 'G':
		if c2 == 'R':
			return 'B'
	return 1 + 'a'

def triangle(x):
	if len(x) == 1:
		return x
	else:
		xx = [get(x[i], x[i + 1]) for i in range(len(x) - 1)]
		return triangle(''.join(xx))


def triangle(row):
	sigue = len(row)
	renant = row
	sigren = ''
	if not 1 <= sigue <= 10 ** 5:
		return 'B'
	while sigue > 1:
		for r in range(0, len(renant) - 1, 1):
			cad = renant[r:r + 2]
			sigren += list({'R', 'G', 'B'} - set(cad) if cad[0] != cad[1] else cad[0])[0]
		renant = sigren
		sigren = ''
		sigue = len(renant)
	return renant


def triangle(row):
	row = ['RGB'.index(char) + 1 for char in row]
	while len(row) > 1:
		row = [row[i] ^ row[i - 1] or row[i] for i in range(1, len(row))]
	return 'RGB'[row[0] - 1]


def triangle(row):
	a = {153: 'B', 148: 'G', 137: 'R', 164: 'R', 142: 'G', 132: 'B', 82: 'R', 71: 'G', 66: 'B'}
	b = list(row)
	while len(b) > 1:
		b = list(map(ord, b))
		b = [a[b[i] + b[i + 1]] for i in range(len(b) - 1)]
	return b[0]


def triangle(row):
	a = len(row)
	print(a)
	if a < 1000:
		while len(row) > 1:
			row = sec(row)
		return row[0]
	return row[0]

def sec(rwo):
	lis = []
	for i in range(len(rwo) - 1):
		lis.append(check(rwo[i], rwo[i + 1]))
	return lis

def check(a, b):
	if a == b:
		return a
	if a == 'R' or b == 'R':
		if a == 'B' or b == 'B':
			return 'G'
		else:
			return 'B'
	return 'R'


repls = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'GB': 'R', 'BR': 'G', 'RG': 'B', 'BG': 'R', 'RB': 'G', 'GR': 'B'}

def reduce1(row):
	return ''.join((repls[row[i:i + 2]] for i in range(len(row) - 1)))

def triangle(row):
	while len(row) > 1:
		row = reduce1(row)
	return row


colours = {'GG': 'G', 'BB': 'B', 'RR': 'R', 'BG': 'R', 'RG': 'B', 'BR': 'G', 'GB': 'R', 'GR': 'B', 'RB': 'G'}

def triangle(row):
	if len(row) > 1:
		row_out = ''
		for i in range(0, len(row) - 1):
			row_out += colours[row[i:i + 2]]
		return triangle(row_out)
	return row


def gen(a, b):
	d = {'BG': 'R', 'BR': 'G', 'GR': 'B'}
	return d.get(''.join(sorted([a, b])), a)

def triangle(row):
	a = [list(' ' * (len(row) - i)) for i in range(len(row))]
	a[0] = list(row)
	for i in range(0, len(row) - 1):
		for j in range(len(a[i]) - 1):
			a[i + 1][j] = gen(a[i][j], a[i][j + 1])
	return a[-1][0]


def triangle(row):
	l = len(row)
	for i in range(l - 1):
		new_row = ''
		for (v, v1) in zip(row, row[1:]):
			if v == v1:
				new_row += v
			else:
				new_row += list({'R', 'G', 'B'} - {v, v1})[0]
		row = new_row
	return row


def triangle(a):
	q = len(a)
	if q == 1:
		return a
	b = 1
	g = 2
	r = 3
	a = a.replace('B', '1')
	a = a.replace('G', '2')
	a = a.replace('R', '3')
	v = int(a)
	count = 0
	while q != 1:
		for i in range(1, q):
			if v // 10 ** (q - i) - 10 * (v // 10 ** (q - i + 1)) == v // 10 ** (q - i - 1) - 10 * (v // 10 ** (q - i)):
				e = v // 10 ** (q - i) - 10 * (v // 10 ** (q - i + 1))
			else:
				e = 6 - (v // 10 ** (q - i) - 10 * (v // 10 ** (q - i + 1)) + v // 10 ** (q - i - 1) - 10 * (v // 10 ** (q - i)))
			count = count + e * 10 ** (q - i - 1)
		q = q - 1
		v = count
		if q == 1:
			if count == 1:
				return 'B'
			elif count == 2:
				return 'G'
			else:
				return 'R'
		count = 0


def triangle(row):
	dir = {'BG': 'R', 'RG': 'B', 'BR': 'G', 'GB': 'R', 'GR': 'B', 'RB': 'G', 'BG': 'R'}
	for _ in range(len(row) - 1):
		row = tuple((dir.get(f'{row[i]}{row[i + 1]}', row[i]) for i in range(len(row) - 1)))
	return row[0]


def triangle(row):
	buffer = ''
	while len(row) > 1:
		aux_string = ''
		for (i, char) in enumerate(row):
			if i + 1 < len(row):
				buffer = char + row[i + 1]
				if char == row[i + 1]:
					aux_string += char
				elif not 'R' in buffer:
					aux_string += 'R'
				elif not 'G' in buffer:
					aux_string += 'G'
				elif not 'B' in buffer:
					aux_string += 'B'
		row = aux_string
	return row


def triangle(row):
	row = row.replace(' ', '')
	next_line = ''
	if len(row) == 1:
		return row
	for index in range(len(row) - 1):
		next_line = next_line + str(color_finder(row[index], row[index + 1]))
	if len(next_line) == 1:
		return next_line
	else:
		return triangle(next_line)

def color_finder(color1, color2):
	colors = ['R', 'G', 'B']
	if color1 == color2:
		return color1
	else:
		colors.remove(color1)
		colors.remove(color2)
		return colors[0]


def triangle(row):
	if len(row) == 1:
		return row[0]
	else:
		for v in range(0, len(row)):
			if v == 0:
				l = row
			else:
				l = lp
			lp = []
			for u in range(0, len(l) - 1):
				ft = l[u]
				st = l[u + 1]
				if ft == st:
					lp.append(ft)
				elif ft == 'R' and st == 'G' or (ft == 'G' and st == 'R'):
					lp.append('B')
				elif ft == 'R' and st == 'B' or (ft == 'B' and st == 'R'):
					lp.append('G')
				elif ft == 'B' and st == 'G' or (ft == 'G' and st == 'B'):
					lp.append('R')
		return l[len(l) - 1]


def triangle(row):
	while len(row) != 1:
		row2 = ''
		for i in range(len(row) - 1):
			if row[i] == row[i + 1]:
				row2 += row[i]
			elif (row[i], row[i + 1]) == ('R', 'B') or (row[i], row[i + 1]) == ('B', 'R'):
				row2 += 'G'
			elif (row[i], row[i + 1]) == ('R', 'G') or (row[i], row[i + 1]) == ('G', 'R'):
				row2 += 'B'
			elif (row[i], row[i + 1]) == ('G', 'B') or (row[i], row[i + 1]) == ('B', 'G'):
				row2 += 'R'
		row = row2
	return row


def triangle(row):
	f = len(row)
	for i in range(f - 1):
		a = ''
		for i in range(len(row) - 1):
			if row[i] == row[i + 1]:
				a += row[i]
			else:
				a += ''.join(set('RGB') - set(row[i]) - set(row[i + 1]))
		row = a[:]
	return row


def triangle(row):
	act = row
	nex = ''
	while len(act) > 2:
		for i in range(len(act) - 1):
			if act[i] == act[i + 1]:
				nex = nex + act[i]
			elif act[i] == 'R' and act[i + 1] == 'G' or (act[i] == 'G' and act[i + 1] == 'R'):
				nex = nex + 'B'
			elif act[i] == 'B' and act[i + 1] == 'G' or (act[i] == 'G' and act[i + 1] == 'B'):
				nex = nex + 'R'
			elif act[i] == 'R' and act[i + 1] == 'B' or (act[i] == 'B' and act[i + 1] == 'R'):
				nex = nex + 'G'
		act = nex
		nex = ''
	if len(act) == 2:
		if act[0] == act[1]:
			return act[0]
		elif act[0] == 'R' and act[1] == 'G' or (act[0] == 'G' and act[1] == 'R'):
			return 'B'
		elif act[0] == 'B' and act[1] == 'G' or (act[0] == 'G' and act[1] == 'B'):
			return 'R'
		elif act[0] == 'R' and act[1] == 'B' or (act[0] == 'B' and act[1] == 'R'):
			return 'G'
	else:
		return act[0]


def triangle(row):
	colors = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'RG': 'B', 'GR': 'B', 'GB': 'R', 'BG': 'R', 'RB': 'G', 'BR': 'G'}
	while not len(row) == 1:
		row = ''.join((colors[row[i] + row[i + 1]] for i in range(len(row) - 1)))
	return row


import math
numberColor = {'R': 0, 'G': 1, 'B': 2}
back = {0: 'R', 1: 'G', 2: 'B'}

def triangle(row):
	if len(row) == 1:
		return row
	n = len(row)
	faqN = math.factorial(n - 1)
	sum = 0
	for k in range(n):
		sum += faqN / (math.factorial(n - k - 1) * math.factorial(k)) * numberColor[row[k]]
	sum *= (-1) ** (n - 1)
	r = sum % 3
	return back[r]


def triangle(row):
	if len(row) == 1:
		return row
	r = ''
	for i in range(1, len(row)):
		if row[i] == row[i - 1]:
			r += row[i]
		else:
			l = ['R', 'G', 'B']
			l.remove(row[i])
			l.remove(row[i - 1])
			r += l[0]
	return triangle(r)


def char_to_put(c1, c2):
	if c1 == c2:
		return c1
	elif c1 == 'B' and c2 == 'R' or (c1 == 'R' and c2 == 'B'):
		return 'G'
	elif c1 == 'G' and c2 == 'R' or (c1 == 'R' and c2 == 'G'):
		return 'B'
	else:
		return 'R'

def compute(s):
	next_row = ''
	for i in range(0, len(s) - 1):
		next_row += char_to_put(s[i], s[i + 1])
	return next_row

def triangle(row):
	if len(row) == 1:
		return row[0]
	while len(row) != 1:
		row = compute(row)
	return row


from re import sub

def triangle(row):
	return row if len(row) == 1 else triangle(''.join((row[i] if row[i] == row[i + 1] else sub(f'[{row[i]}{row[i + 1]}]', '', 'RGB') for i in range(len(row) - 1))))


def triangle(k):
	s = ''
	if len(k) == 1:
		return k
	for i in range(len(k) - 1):
		if k[i] == k[i + 1]:
			s += k[i]
		elif k[i] == 'R' and k[i + 1] == 'B' or (k[i] == 'B' and k[i + 1] == 'R'):
			s += 'G'
		elif k[i] == 'R' and k[i + 1] == 'G' or (k[i] == 'G' and k[i + 1] == 'R'):
			s += 'B'
		elif k[i] == 'G' and k[i + 1] == 'B' or (k[i] == 'B' and k[i + 1] == 'G'):
			s += 'R'
	print(s)
	while len(s) > 0:
		if len(s) == 1:
			return s
		s1 = s
		s = ''
		for i in range(len(s1) - 1):
			if s1[i] == s1[i + 1]:
				s += s1[i]
			elif s1[i] == 'R' and s1[i + 1] == 'B' or (s1[i] == 'B' and s1[i + 1] == 'R'):
				s += 'G'
			elif s1[i] == 'R' and s1[i + 1] == 'G' or (s1[i] == 'G' and s1[i + 1] == 'R'):
				s += 'B'
			elif s1[i] == 'G' and s1[i + 1] == 'B' or (s1[i] == 'B' and s1[i + 1] == 'G'):
				s += 'R'


def triangle(row):
	print(row)
	r = ''
	a = row[:]
	if len(a) == 1:
		return a
	for i in range(len(a) - 1):
		t = 'RGB'
		if a[i] == a[i + 1]:
			r += a[i]
		else:
			t = t.replace(a[i], '').replace(a[i + 1], '')
			r += t
	return triangle(r)


def colour(a, b):
	if a == b:
		return a
	elif a == 'B' and b == 'G' or (a == 'G' and b == 'B'):
		return 'R'
	elif a == 'R' and b == 'G' or (a == 'G' and b == 'R'):
		return 'B'
	elif a == 'B' and b == 'R' or (a == 'R' and b == 'B'):
		return 'G'

def triangle(row):
	if len(row) < 2:
		return row
	else:
		rez = []
		for i in range(0, len(row) - 1):
			rez.append(colour(row[i], row[i + 1]))
		l = len(rez)
		if l > 1:
			rez = triangle(rez)
		return ''.join(rez)


def triangle(row):
	colors = set(['B', 'G', 'R'])
	if len(row) == 1:
		return row
	else:
		ans = ''
		for i in range(len(row) - 1):
			current = row[i]
			next = row[i + 1]
			color_set = set([current, next])
			if current == next:
				new_color = current
				ans = ans + new_color
			else:
				new_color = list(colors - color_set)[0]
				ans = ans + new_color
	return triangle(ans)


def triangle(row):
	reduction = [3 ** i + 1 for i in range(10) if 3 ** i <= 100000][::-1]
	for j in reduction:
		while len(row) >= j:
			row = [row[i] if row[i] == row[i + j - 1] else ({'R', 'G', 'B'} - {row[i], row[i + j - 1]}).pop() for i in range(len(row) - j + 1)]
	return row[0]


def triangle(row):
	colors = ['R', 'G', 'B']
	result = list(row)
	while len(result) > 1:
		current = list()
		for (i, j) in zip(result[:-1], result[1:]):
			if i == j:
				current.extend([i])
			else:
				for c in colors:
					if c not in [i, j]:
						current.extend([c])
		result = current
	return result[0]


import itertools
LOOKUP = {}

def slow_lookup(x, y):
	if x == y:
		return x
	rem = set('RGB') - {x, y}
	print(x, y, rem)
	return rem.pop()
for x in 'RGB':
	for y in 'RGB':
		LOOKUP[x + y] = slow_lookup(x, y)
fast_lookup = lambda x, y: LOOKUP(x + y)

def triangle(row):
	print(row)
	for _ in range(len(row) - 1):
		row = ''.join((LOOKUP[row[x:x + 2]] for x in range(len(row) - 1)))
		print(row)
	return next(iter(row))


colorReverDic = {'RR': 'R', 'RG': 'B', 'RB': 'G', 'GG': 'G', 'GR': 'B', 'GB': 'R', 'BB': 'B', 'BR': 'G', 'BG': 'R'}

def triangle(row):
	newRow = row
	for i in range(len(row) - 1):
		l = len(newRow) - 1
		workRow = ''
		for c in range(l):
			workRow += colorReverDic[newRow[c] + newRow[c + 1]]
		newRow = workRow
	return newRow


def triangle(inp):
	kek = inp
	while len(kek) != 1:
		mek = []
		for (c1, c2) in zip(kek, kek[1:]):
			if c1 == c2:
				mek.append(c1)
			else:
				lol = ['R', 'G', 'B']
				lol.remove(c1)
				lol.remove(c2)
				mek.append(*lol)
		kek = mek
	return kek[0]


def mod(a, b):
	r = a % b
	r = r + b if r < 0 else r
	print(r)
	return r

def ColorToInt(c):
	if c == 'R':
		return 0
	if c == 'G':
		return 1
	if c == 'B':
		return 2
	return -1

def IntToColor(i):
	if i == 0:
		return 'R'
	if i == 1:
		return 'G'
	if i == 2:
		return 'B'
	return 'E'

def triangle(row):
	end = len(row) - 1
	i = 0
	seq = list(row)
	while end > 0:
		seq[i] = IntToColor(mod(-ColorToInt(seq[i]) - ColorToInt(seq[i + 1]), 3))
		i += 1
		if i == end:
			end -= 1
			i = 0
	return seq[0]


def triangle(row):
	if len(row) == 1:
		return row
	while len(row) > 1:
		N = len(row)
		new_row = []
		i = 0
		while i < N - 1:
			word = [row[i], row[i + 1]]
			word.sort()
			word = ''.join(word)
			if word == 'BG':
				new_row.append('R')
			elif word == 'GR':
				new_row.append('B')
			elif word == 'BR':
				new_row.append('G')
			elif word == 'GG':
				new_row.append('G')
			elif word == 'RR':
				new_row.append('R')
			elif word == 'BB':
				new_row.append('B')
			else:
				return False
			i += 1
		row = new_row
	return row[0]


def triangle(row):
	next_row = row
	for i in range(len(row)):
		row = next_row
		next_row = []
		for j in range(len(row) - 1):
			next_row.append(clr_add(row[j], row[j + 1]))
	return row[0]

def clr_add(a, b):
	if a is b:
		return a
	if a is 'R' and b is 'G' or (a is 'G' and b is 'R'):
		return 'B'
	if a is 'G' and b is 'B' or (a is 'B' and b is 'G'):
		return 'R'
	if a is 'B' and b is 'R' or (a is 'R' and b is 'B'):
		return 'G'


C = {'GG': 'G', 'RR': 'R', 'BB': 'B', 'BG': 'R', 'GB': 'R', 'RG': 'B', 'GR': 'B', 'RB': 'G', 'BR': 'G'}

def triangle(row: str):
	tmp_row = []
	while len(row) != 1:
		for i in range(len(row) - 1):
			tmp_row.append(C.get(row[i:i + 2]))
		row = ''.join(tmp_row)
		tmp_row.clear()
	return row[0]


colors = 'RGB'

def triangle(row):
	if len(row) <= 1:
		return row
	if len(row) == 2:
		if row[0] == row[1]:
			return row[0]
		else:
			return colors.replace(row[0], '').replace(row[1], '')
	return triangle(''.join(list(map(triangle, [row[i:i + 2] for i in range(len(row) - 1)]))))


def triangle(row):
	(place, y) = (0, '')
	while len(row) > 1:
		(place, y) = (0, '')
		while place < len(row) - 1:
			if row[place] == row[place + 1]:
				y += row[place]
			if row[place] == 'R' and row[place + 1] == 'B':
				y += 'G'
			if row[place] == 'B' and row[place + 1] == 'R':
				y += 'G'
			if row[place] == 'R' and row[place + 1] == 'G':
				y += 'B'
			if row[place] == 'G' and row[place + 1] == 'R':
				y += 'B'
			if row[place] == 'G' and row[place + 1] == 'B':
				y += 'R'
			if row[place] == 'B' and row[place + 1] == 'G':
				y += 'R'
			place += 1
		row = y
	return row


def triangle(x):
	(c, r) = (['R', 'G', 'B'], [])
	if len(x) == 1:
		return x[0]
	for i in range(len(x) - 1):
		if x[i] == x[i + 1]:
			r.append(x[i])
		else:
			r += [e for e in c if e not in [x[i], x[i + 1]]]
	return triangle(r)


mix = {('G', 'G'): 'G', ('G', 'R'): 'B', ('G', 'B'): 'R', ('B', 'G'): 'R', ('B', 'R'): 'G', ('B', 'B'): 'B', ('R', 'G'): 'B', ('R', 'R'): 'R', ('R', 'B'): 'G'}

def triangle(row):
	if len(row) == 1:
		return row
	r = ''
	for c in range(len(row) - 1):
		r += mix.get((row[c], row[c + 1]), '')
	return triangle(r)


def triangle(row):
	return row if len(row) == 1 else triangle(''.join(process(row)))

def process(row):
	if len(row) == 1:
		yield row
	else:
		row = list(row)
		current_color = row.pop(0)
		while len(row) > 0:
			next_color = row.pop(0)
			yield color(current_color + next_color)
			current_color = next_color

def color(row):
	if len(row) == 1:
		return row
	if row in 'RR GG BB':
		return row[0]
	elif row in 'RG GR':
		return 'B'
	elif row in 'GB BG':
		return 'R'
	elif row in 'RB BR':
		return 'G'
	raise ValueError(row)


import operator as op
from functools import reduce

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def triangle(row):
	dict = {'R': 0, 'G': 1, 'B': 2}
	dict2 = {0: 'R', 1: 'G', 2: 'B'}
	if len(row) % 2 == 0:
		x = -1
	else:
		x = 1
	sum = 0
	n = len(row) - 1
	k = 0
	for i in row:
		sum += ncr(n, k) * dict[i]
		print(ncr(n, k), dict[i])
		k += 1
	return dict2[x * sum % 3]


def nextt(a, b):
	if a == b:
		return a
	return 'RGB'.replace(a, '').replace(b, '')

def triangle(row):
	while len(row) > 1:
		t = ''
		for i in range(len(row) - 1):
			t = t + nextt(list(row)[i], list(row)[i + 1])
		row = t
	return row[0]


def next_char(pair):
	return set('RGB').difference(pair).pop() if pair[0] != pair[1] else pair[0]

def next_row(row):
	return ''.join(map(next_char, zip(row, row[1:])))

def triangle(row):
	while len(row) > 1:
		row = next_row(row)
	return row


colors = {'R', 'G', 'B'}

def pair(v):
	dif = colors.difference(v)
	return v[0] if len(dif) == 2 else dif.pop()

def triangle(row):
	if len(row) == 1:
		return row[0]
	next_row = []
	for i in range(len(row) - 1):
		next_row.append(pair(row[i:i + 2]))
	return triangle(next_row)


def triangle(row):
	if len(row) == 1:
		return row
	colors = {'GB': 'R', 'BG': 'R', 'GG': 'G', 'RR': 'R', 'BB': 'B', 'RB': 'G', 'BR': 'G', 'RG': 'B', 'GR': 'B'}
	zipped = list(zip(row, row[1:]))
	row = ''
	for x in zipped:
		row += colors[''.join(x)]
	return triangle(row)


def triangle(row):
	if len(row) == 1:
		return row
	new = ''
	for i in range(len(row) - 1):
		if row[i] == row[i + 1]:
			new += row[i]
		elif row[i] == 'R' and row[i + 1] == 'G':
			new += 'B'
		elif row[i] == 'G' and row[i + 1] == 'R':
			new += 'B'
		elif row[i] == 'G' and row[i + 1] == 'B':
			new += 'R'
		elif row[i] == 'B' and row[i + 1] == 'G':
			new += 'R'
		elif row[i] == 'R' and row[i + 1] == 'B':
			new += 'G'
		elif row[i] == 'B' and row[i + 1] == 'R':
			new += 'G'
	return triangle(new)


conv = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'RG': 'B', 'GR': 'B', 'RB': 'G', 'BR': 'G', 'BG': 'R', 'GB': 'R'}

def triangle(row):

	def dec(row):
		return ''.join((conv[row[x:x + 2]] for x in range(len(row) - 1)))
	while len(row) - 1:
		row = dec(row)
	return row


cash = {'R': 'R', 'G': 'G', 'B': 'B', 'RR': 'R', 'RG': 'B', 'RB': 'G', 'GR': 'B', 'GG': 'G', 'GB': 'R', 'BR': 'G', 'BG': 'R', 'BB': 'B'}

def triangle(row):
	result = cash.get(row, False)
	if not result:
		result = cash[triangle(row[:-1]) + triangle(row[1:])]
		cash.update({row: result})
		return result
	return result


def triangle(row):
	to_num = {'R': 1, 'G': 2, 'B': 3}
	from_num = {1: 'R', 2: 'G', 3: 'B'}
	row_num = [to_num[y] for y in row]
	while len(row_num) > 1:
		t = []
		for x in range(len(row_num) - 1):
			if row_num[x] == row_num[x + 1]:
				t.append(row_num[x])
			else:
				t.append(6 - row_num[x] - row_num[x + 1])
		row_num = t
	return from_num[row_num[0]]


def next(a, b):
	return 3 - (a + b) % 3

def rgb_to_number(string):
	rgb = ['R', 'G', 'B']
	return [rgb.index(x) + 1 for x in string]

def triangle(row):
	number_dict = {1: 'R', 2: 'G', 3: 'B'}
	row = rgb_to_number(row)
	while len(row) > 1:
		row = [next(row[i], row[i + 1]) for i in range(len(row) - 1)]
	return number_dict[row[0]]


def triangle(row):
	MAP = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'RG': 'B', 'RB': 'G', 'GR': 'B', 'GB': 'R', 'BR': 'G', 'BG': 'R'}
	while len(row) > 1:
		row = ''.join([MAP[''.join(x)] for x in zip(row, row[1:])])
	return row


def compare(pair):
	if len(set(pair)) == 1:
		return pair[0]
	return list(set(pair) ^ set('RGB'))[0]

def triangle(row):
	return triangle([compare([row[i], row[i + 1]]) for i in range(len(row) - 1)]) if len(row) > 1 else row[0]


def triangle(row):
	for i in range(0, len(row) - 1):
		res = []
		for k in range(0, len(row) - 1):
			if row[k] == row[k + 1]:
				res += row[k]
			else:
				res += 'GBR'.replace(row[k], '').replace(row[k + 1], '')
		row = res
	return row[0]
	pass


from math import ceil

def memoize(f):
	memo = {}

	def _(x):
		if x not in memo:
			memo[x] = f(x)
		return memo[x]
	return _

@memoize
def reduce(row):
	MAP = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'RG': 'B', 'RB': 'G', 'GR': 'B', 'GB': 'R', 'BR': 'G', 'BG': 'R'}
	CHUNK = 16
	len_ = len(row)
	if len_ == 1:
		return row
	elif len_ == 2:
		return MAP[row]
	elif len_ > CHUNK + 1:
		n = ceil(len_ / CHUNK)
		return ''.join([reduce(row[CHUNK * i:CHUNK * (i + 1) + 1]) for i in range(n)])
	else:
		return ''.join([MAP[row[i:i + 2]] for i in range(len_ - 1)])

def triangle(row):
	while len(row) > 1:
		row = reduce(row)
	return row


def triangle(row):
	k = 0
	while k < len(row):
		if row[k] == 'G' or row[k] == 'B' or row[k] == 'R':
			k += 1
			continue
		else:
			return print(' only R,G,B are allowed')
	if len(row) == 1:
		return row
	how_many = len(row) - 1
	lenght = len(row)
	new_row = ''
	i = 0
	j = 0
	while j < how_many:
		j = +1
		while True:
			if len(new_row) == len(row) - 1:
				if len(new_row) == 1:
					return new_row
				row = new_row
				new_row = ''
				lenght = len(row)
				i = 0
				break
			if row[i] == row[i + 1]:
				new_row = new_row + row[i]
				i += 1
				continue
			elif row[i] == 'B' and row[i + 1] == 'G' or (row[i] == 'G' and row[i + 1] == 'B'):
				new_row = new_row + 'R'
				i += 1
				continue
			elif row[i] == 'R' and row[i + 1] == 'G' or (row[i] == 'G' and row[i + 1] == 'R'):
				new_row = new_row + 'B'
				i += 1
				continue
			elif row[i] == 'B' and row[i + 1] == 'R' or (row[i] == 'R' and row[i + 1] == 'B'):
				new_row = new_row + 'G'
				i += 1
				continue
			if len(new_row) == 1:
				return new_row
			else:
				row = new_row
				i = 0


def memoize(f):
	memo = {}

	def _(x):
		if x not in memo:
			memo[x] = f(x)
		return memo[x]
	return _

@memoize
def reduce(row):
	MAP = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'RG': 'B', 'RB': 'G', 'GR': 'B', 'GB': 'R', 'BR': 'G', 'BG': 'R'}
	len_ = len(row)
	if len_ == 1:
		return row
	elif len_ == 2:
		return MAP[row]
	else:
		i = len_ // 2
		return reduce(row[0:i + 1]) + reduce(row[i:len_])

def triangle(row):
	while len(row) > 1:
		row = reduce(row)
	return row


def triangle(row):
	MAP = {'RR': 'R', 'GG': 'G', 'BB': 'B', 'RG': 'B', 'RB': 'G', 'GR': 'B', 'GB': 'R', 'BR': 'G', 'BG': 'R'}
	while len(row) > 1:
		row = [MAP[row[i] + row[i + 1]] for i in range(len(row) - 1)]
	return row[0]


def k_color_code(a, b):
	i = 0
	j = 0
	if a.lower() == 'r':
		i = 1
	if a.lower() == 'b':
		i = 0.1
	if a.lower() == 'g':
		i = 0.01
	if b.lower() == 'r':
		j = 1
	if b.lower() == 'b':
		j = 0.1
	if b.lower() == 'g':
		j = 0.01
	return i + j

def k_color(k_code):
	if k_code == 0.02:
		return 'g'
	elif k_code == 0.2:
		return 'b'
	elif k_code == 2:
		return 'r'
	elif k_code == 1.01:
		return 'b'
	elif k_code == 1.1:
		return 'g'
	elif k_code == 0.11:
		return 'r'
	elif k_code == 0.11:
		return 'r'
	else:
		return 'n'

def new_row(row):
	s = row
	ps = ''
	x = s
	for i in range(0, len(x)):
		if i + 1 != len(x):
			ps += k_color(k_color_code(x[i], x[i + 1]))
		else:
			break
	x = ps
	return x

def triangle(row):
	s = row
	while len(s) != 1:
		s = new_row(s)
	return s.upper()


import functools, itertools

def triangle(row):

	def next_level(level_row):
		if len(level_row) > 2:
			return ''.join([next_level(level_row[i:i + 2]) for i in range(len(level_row)) if len(level_row[i:i + 2]) > 1])
		str_list = ''.join(sorted([level_row[0], level_row[1]]))
		if level_row[0] == level_row[1]:
			return level_row[0]
		return {'GR': 'B', 'BG': 'R', 'BR': 'G'}.get(str_list)
	result = row
	while len(result) > 1:
		result = next_level(result)
	return result


red = 'R'
blu = 'B'
gre = 'G'

def add(col1, col2):
	if col1 == col2:
		return col1
	elif (col1 == blu or col1 == gre) and (col2 == blu or col2 == gre):
		return red
	elif (col1 == red or col1 == gre) and (col2 == red or col2 == gre):
		return blu
	elif (col1 == blu or col1 == red) and (col2 == blu or col2 == red):
		return gre

def triangle(row):
	if row == None:
		return None
	row = list(row)
	l = len(row)
	if l == 0:
		return None
	if l == 1:
		return row[0]
	if l == 2:
		return add(row[0], row[1])
	for i in range(0, l - 1):
		row[i] = add(row[i], row[i + 1])
	row.pop()
	return triangle(row)


def new_row(row):
	combinations = ['BG', 'GB', 'RG', 'GR', 'BR', 'RB']
	res = ['R', 'R', 'B', 'B', 'G', 'G']
	row2 = ''
	for i in range(len(row) - 1):
		if row[i] == row[i + 1]:
			row2 += row[i]
		else:
			row2 += res[combinations.index(row[i] + row[i + 1])]
	return row2

def triangle(row):
	if len(row) == 1:
		return row
	return triangle(new_row(row))


def triangle(row):
	str1 = ''
	i = len(row)
	while i > 1:
		for j in range(i - 1):
			if row[j] == row[j + 1]:
				str1 += row[j]
			elif row[j] != row[j + 1]:
				if row[j] == 'R' and row[j + 1] == 'G':
					str1 += 'B'
				elif row[j] == 'R' and row[j + 1] == 'B':
					str1 += 'G'
				elif row[j] == 'G' and row[j + 1] == 'B':
					str1 += 'R'
				elif row[j] == 'G' and row[j + 1] == 'R':
					str1 += 'B'
				elif row[j] == 'B' and row[j + 1] == 'R':
					str1 += 'G'
				elif row[j] == 'B' and row[j + 1] == 'G':
					str1 += 'R'
		row = str1
		str1 = ''
		i = len(row)
	return row