Skip to content

Instantly share code, notes, and snippets.

@soimort
Last active December 16, 2015 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soimort/5387922 to your computer and use it in GitHub Desktop.
Save soimort/5387922 to your computer and use it in GitHub Desktop.
GCJ 2013 Qualification Round - Problem A
# Authored by soimort
import sys
def test_status(lst):
count = {'X': 0, 'O': 0, 'T': 0, '.': 0}
for elem in lst:
count[elem] += 1
if count['X'] == 4 or count['X'] == 3 and count['T'] == 1:
return 'X'
if count['O'] == 4 or count['O'] == 3 and count['T'] == 1:
return 'O'
if count['.'] > 0:
return '.'
return ''
caseAmount = int(sys.stdin.readline())
for caseNum in range(1, caseAmount + 1):
m = [list(sys.stdin.readline()[:-1])
for i in range(0, 4)]
sys.stdin.readline() # skip a line break
t = ''
for i in range(0, 4):
t += test_status(m[i])
for i in range(0, 4):
t += test_status([m[j][i] for j in range(0, 4)])
t += test_status([m[i][i] for i in range(0, 4)])
t += test_status([m[i][3 - i] for i in range(0, 4)])
if t == '':
result = 'Draw'
elif t.find('X') >= 0:
result = 'X won'
elif t.find('O') >= 0:
result = 'O won'
else:
result = 'Game has not completed'
print("Case #%s: %s" % (caseNum, result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment