Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Created January 22, 2012 05:48
Show Gist options
  • Save krrrr38/1655812 to your computer and use it in GitHub Desktop.
Save krrrr38/1655812 to your computer and use it in GitHub Desktop.
人材獲得作戦2011 - ぷよぷよ
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import copy
def usage():
print "usage: %s puyomap_file" % sys.argv[0]
return 1
def count_width_height(puyomap):
return (len(puyomap[0]), len(puyomap))
def showmap(puyomap):
(w, h) = count_width_height(puyomap)
for line in puyomap:
print " " , "".join(line), " "
print "*" * (w+4)
def make_connect_list(h, w, value, puyomap, connect_list):
(max_w, max_h) = count_width_height(puyomap)
if w < max_w-2:
if puyomap[h][w+1] == value:
connect_list.append((h,w+1))
connect_list = make_connect_list(h, w+1, value, puyomap, connect_list)
if h < max_h-2:
if puyomap[h+1][w] == value:
connect_list.append((h+1,w))
connect_list = make_connect_list(h+1, w, value, puyomap, connect_list)
if w > 0:
if (h, w-1) in connect_list:
return connect_list
if puyomap[h][w-1] == value:
connect_list.append((h,w-1))
connect_list = make_connect_list(h, w-1, value, puyomap, connect_list)
if h > 0:
if (h-1, w) in connect_list:
return connect_list
if puyomap[h-1][w] == value:
connect_list.append((h-1,w))
connect_list = make_connect_list(h-1, w, value, puyomap, connect_list)
return connect_list
def remove_repetition(delete_list):
ret_list = []
for l in delete_list:
if list(l) not in ret_list:
ret_list.append(list(l))
return ret_list
def search_delete_list(puyomap):
delete_list = []
for h, line in enumerate(puyomap):
for w, value in enumerate(line):
if value == ' ':
continue
connect_list = make_connect_list(h, w, value, puyomap, [(h,w)])
delete_list.append(set(connect_list))
delete_list = filter(lambda l: len(l) > 3, delete_list)
return remove_repetition(delete_list)
def delete_puyo(puyomap, delete_list):
for deletes in delete_list:
for h, w in deletes:
puyomap[h][w] = ' '
def drop_puyo(puyomap):
(max_w, max_h) = count_width_height(puyomap)
while True:
tmp_puyomap = copy.deepcopy(puyomap)
for h in range(max_h-1):
for w in range(max_w):
if puyomap[h+1][w] == ' ':
puyomap[h+1][w] = puyomap[h][w]
puyomap[h][w] = ' '
if tmp_puyomap == puyomap:
break
def next_map(puyomap,delete_list):
delete_puyo(puyomap, delete_list)
drop_puyo(puyomap)
def main(file):
puyomap = []
with open(file) as f:
for line in f:
line = line.replace('\n','')
puyomap.append(list(line))
combo_count = 0
while True:
showmap(puyomap)
delete_list = search_delete_list(copy.deepcopy(puyomap))
if not delete_list:
return 1
next_map(puyomap, delete_list)
print "combo : ", combo_count
combo_count += 1
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit(usage())
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment