Skip to content

Instantly share code, notes, and snippets.

@okaq
Created May 24, 2011 15:33
Show Gist options
  • Save okaq/988924 to your computer and use it in GitHub Desktop.
Save okaq/988924 to your computer and use it in GitHub Desktop.
Solution: Square Tiles(Google Code Jam 2011 Round 1C Problem A)
import sys
# files
fin = file(sys.argv[1])
fout = open(sys.argv[2], 'w')
lines = fin.readlines()
tests = int(lines[0])
# parse
indexes = [1]
wh = []
for i in range(tests):
wh.append(map(int, lines[indexes[i]].split()))
indexes.append(indexes[i] + wh[i][0] + 1)
# tiles
tiles = []
for i in range(tests):
index = indexes[i]
h = wh[i][0]
# w = wh[i][1]
tile = {}
for j in range(h):
tile[j] = lines[index + j + 1].split()
tiles.append(tile)
# switch
def blue(c0):
if c0 == '#':
return True
return False
def get(x0, y0, t0):
r0 = None
try:
r0 = t0[y0][0][x0]
except ValueError:
r0 = None
finally:
return r0
def quad(x0, y0, t0):
xy = [(x0,y0),(x0+1,y0),(x0,y0+1),(x0+1,y0+1)]
q0 = []
for p0 in xy:
q0.append(get(p0[0],p0[1],t0))
return q0
def switch(x0, y0, t0):
q0 = quad(x0, y0, t0)
if len(filter(lambda x: x, map(blue, q0))) == 4:
r0 = list(t0[y0][0])
r1 = list(t0[y0+1][0])
""" python str immutable
t0[y0][0][x0] = '/'
t0[y0][0][x0+1] = '\\'
t0[y0+1][0][x0] = '\\'
t0[y0+1][0][x0+1] = '/'
"""
r0[x0] = '/'
r0[x0+1] = '|' # backslash hack ;)
r1[x0] = '|'
r1[x0+1] = '/'
t0[y0][0] = "".join(r0)
t0[y0+1][0] = "".join(r1)
def output(t0, c0):
fout.write("Case #%d:\n" % c0)
imp = False
for y in t:
for x in range(len(t[y][0])):
if blue(t[y][0][x]):
imp = True
break
else:
continue
break
if (imp):
fout.write("Impossible")
else:
for y in t:
for x in range(len(t[y][0])):
if t[y][0][x] == '|':
fout.write('\\')
else:
fout.write(t[y][0][x])
fout.write('\n')
fout.write('\n')
c = 1
for t in tiles:
for y in t:
for x in range(len(t[y][0])):
if blue(t[y][0][x]):
switch(x, y, t)
output(t, c)
c += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment