Skip to content

Instantly share code, notes, and snippets.

@okaq
Created May 9, 2011 01:12
Show Gist options
  • Save okaq/961876 to your computer and use it in GitHub Desktop.
Save okaq/961876 to your computer and use it in GitHub Desktop.
Solution: Bot Trust (Google Code Jam 2011 Qualification Round Problem A)
import sys
fin = file(sys.argv[1])
fout = open(sys.argv[2], 'w')
lines = fin.readlines()
tests = int(lines[0])
def getNext(c):
for step in range(index, 2 * steps, 2):
if ca[step] == c:
return int(ca[step+1])
if c == 'B':
return bP
if c == 'O':
return oP
for test in range(1, tests+1):
ca = lines[test].split()
steps = int(ca[0])
bT = 0
oT = 0
bP = 1
oP = 1
tick = 0
run = True
index = 1
bN = getNext('B')
oN = getNext('O')
while run:
# update timer
tick += 1
# check end condition
if index == (2 * steps - 1):
if ca[index] == 'B':
if bP == int(ca[index+1]):
run = False
continue
if ca[index] == 'O':
if oP == int(ca[index+1]):
run = False
continue
# blue bot logic
if ca[index] == 'B':
if bP == int(ca[index+1]):
if oP > oN:
oP -= 1
if oP < oN:
oP += 1
index += 2
bN = getNext('B')
continue
if bP > int(ca[index+1]):
bP -= 1
if bP < int(ca[index+1]):
bP += 1
if oP > oN:
oP -= 1
if oP < oN:
oP += 1
continue
# orange bot logic
if ca[index] == 'O':
if oP == int(ca[index+1]):
if bP > bN:
bP -= 1
if bP < bN:
bP += 1
index += 2
oN = getNext('O')
continue
if oP > int(ca[index+1]):
oP -= 1
if oP < int(ca[index+1]):
oP += 1
if bP > bN:
bP -= 1
if bP < bN:
bP += 1
continue
fout.write('Case #%d: %d\n' % (test, tick))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment