Created
May 22, 2011 13:23
-
-
Save lprsd/985458 to your computer and use it in GitHub Desktop.
Google Code Jam 2011 Round 1C, solution in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from copy import copy | |
def get_count(new_art, a): | |
return sum([el.count(a) for el in new_art]) | |
def format_output(tiles): | |
nr = [] | |
for r in tiles: | |
nr.append("".join(r)) | |
tr_str = "\n".join(nr) | |
tr_str = tr_str.replace('1','/') | |
tr_str = tr_str.replace('2','\\') | |
tr_str = tr_str.replace('3','\\') | |
tr_str = tr_str.replace('4','/') | |
return tr_str | |
def edit_tiles(): | |
new_art = [list(el) for el in art] | |
for i, r in enumerate(new_art): | |
for j, c in enumerate(r): | |
if new_art[i][j] != '#': | |
continue | |
if i>0 and new_art[i-1][j] == '1': | |
new_art[i][j] = "3" | |
elif i>0 and new_art[i-1][j] == '2': | |
new_art[i][j] = '4' | |
elif j>0 and new_art[i][j-1] == '1': | |
new_art[i][j] = '2' | |
else: | |
new_art[i][j] = '1' | |
if get_count(new_art,'1') == get_count(new_art,'2') == get_count(new_art, '3') == get_count(new_art, '4'): | |
return format_output(new_art) | |
else: | |
return 'Impossible' | |
def test_cases(): | |
with open("in2.txt") as f: | |
size = int(f.readline()) | |
#print 'size=%s'%size | |
for i in range(size): | |
global rows, columns | |
rows, columns = [int(el) for el in f.readline().split()] | |
#print rows, columns | |
global art | |
art = [] | |
for j in range(rows): | |
art.append(f.readline().strip()) | |
print "Case #%s:"%(i+1) | |
print "%s"%edit_tiles() | |
test_cases() | |
#print get_win_percentage('.11.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment