Skip to content

Instantly share code, notes, and snippets.

@enaeseth
Created March 16, 2012 00:34
Show Gist options
  • Save enaeseth/2047884 to your computer and use it in GitHub Desktop.
Save enaeseth/2047884 to your computer and use it in GitHub Desktop.
# This code is horrible but you wanted the computation to be done in a single
# generator expression. Cheers!
import json
import sys
def resize(grid):
for line in grid:
line.extend(['.'] * 3)
header = [['.'] * 10] * 3
grid.extend(header)
header.extend(grid)
return header
def search(file):
grid = json.load(file)
line_count = len(grid)
line_length = len(grid[0])
grid = resize(grid)
for result in (set(grid[i+m][j+n] for m, n in zip(iter1, iter2))
for iter1, iter2 in (([0] * 4, range(4)), (range(4), [0] * 4),
(range(4), range(4)), (range(0, -4, -1), range(4)))
for i in range(3, line_count+3) for j in range(line_length)):
if result == {'O'}:
return 'Winner: O'
elif result == {'X'}:
return 'Winner: X'
return 'No winner'
if __name__ == '__main__':
print(search(sys.stdin))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment