Skip to content

Instantly share code, notes, and snippets.

@peroon
Last active April 23, 2017 14:58
Show Gist options
  • Select an option

  • Save peroon/c2f12647a6ba4dc04a0371c356950943 to your computer and use it in GitHub Desktop.

Select an option

Save peroon/c2f12647a6ba4dc04a0371c356950943 to your computer and use it in GitHub Desktop.
Google Code Jam Online Round 1: Sub-Round B-B-large April 22, 2017 16:00 UTC – 18:30 UTC https://code.google.com/codejam/contest/8294486/dashboard#s=p1
# -*- coding: utf-8 -*-
import sys
import os
input_text_path = __file__.replace('.py', '.txt')
fd = os.open(input_text_path, os.O_RDONLY)
os.dup2(fd, sys.stdin.fileno())
f = open('submit.txt', 'w')
T = int(input())
for t in range(T):
N, R, O, Y, G, B, V = list(map(int, input().split()))
impossible = False
# cant wrap but circle meets condition
if V > 0 and V == Y and R == O == G == B == 0:
answer = 'VY' * V
elif O > 0 and O == B and V == Y == G == R == 0:
answer = 'OB' * O
elif G > 0 and G == R and O == B == V == Y == 0:
answer = 'GR' * G
# cant wrap
elif (O > 0 and O >= B) or (V > 0 and V >= Y) or (G > 0 and G >= R):
answer = "IMPOSSIBLE"
else:
# after wrap
if O != 0:
B -= (O + 1) # consume to wrap
B += 1 # recognize as B
if V != 0:
Y -= (V + 1)
Y += 1
if G != 0:
R -= (G + 1)
R += 1
# can consume RYB?
if R == 0 and Y != B:
impossible = True
answer = 'IMPOSSIBLE'
if Y == 0 and R != B:
impossible = True
answer = 'IMPOSSIBLE'
if B == 0 and R != Y:
impossible = True
answer = 'IMPOSSIBLE'
if not impossible:
min_val = min(R, Y, B)
rest_R = R - min_val
rest_Y = Y - min_val
rest_B = B - min_val
# insert order by little
if R == 0 and Y == B:
s = 'YB' * Y
rest_R = rest_Y = rest_B = 0
elif Y == 0 and R == B:
s = 'RB' * R
rest_R = rest_Y = rest_B = 0
elif B == 0 and R == Y:
s = 'RY' * Y
rest_R = rest_Y = rest_B = 0
else:
s = 'RYB' * min_val
while not (rest_R == 0 and rest_Y == 0 and rest_B == 0):
# R
if rest_R > 0:
if 'YB' in s:
s = s.replace('YB', 'YRB', 1)
rest_R -= 1
elif 'BY' in s:
s = s.replace('BY', 'BRY', 1)
rest_R -= 1
elif (s[0] == 'B' and s[-1] == 'Y') or (s[0] == 'Y' and s[-1] == 'B'):
s = 'R' + s
rest_R -= 1
else:
impossible = True
break
# Y
if rest_Y > 0:
if 'RB' in s:
s = s.replace('RB', 'RYB', 1)
rest_Y -= 1
elif 'BR' in s:
s = s.replace('BR', 'BYR', 1)
rest_Y -= 1
elif (s[0] == 'R' and s[-1] == 'B') or (s[0] == 'B' and s[-1] == 'R'):
s = 'Y' + s
rest_Y -= 1
else:
impossible = True
break
# B
if rest_B > 0:
if 'RY' in s:
s = s.replace('RY', 'RBY', 1)
rest_B -= 1
elif 'YR' in s:
s = s.replace('YR', 'YBR', 1)
rest_B -= 1
elif (s[0] == 'Y' and s[-1] == 'R') or (s[0] == 'R' and s[-1] == 'Y'):
s = 'B' + s
rest_B -= 1
else:
impossible = True
break
if not impossible:
# lastly, replace for O, G, V
for i in range(O):
s = s.replace('B', 'BOB', 1)
for i in range(G):
s = s.replace('R', 'RGR', 1)
for i in range(V):
s = s.replace('Y', 'YVY', 1)
answer = s
else:
answer = "IMPOSSIBLE"
print(t, 'answer', answer)
f.write("Case #{}: {}\n".format(t+1, answer))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment