Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
i_love_regexp solution in Creative category for Safe Coasts by Kerulen
import re;
def finish_map(regional_map):
l = len(regional_map[0])
my_map=re.sub('(?s)(?<=X{1})\.|(?<=X{1}.{'+str(l-1)+'})\.|(?<=X{1}.{'+str(l)+'})\.|(?<=X{1}.{'+str(l+1)+'})\.|\.(?=X{1})|\.(?=.{'+str(l-1)+'}X{1})|\.(?=.{'+str(l)+'}X{1})|\.(?=.{'+str(l+1)+'}X{1})','S','\n'.join(regional_map))
old_map=''
while old_map!=my_map:
old_map=my_map[:]
my_map=re.sub('(?s)(?<=D{1})\.|(?<=D{1}.{'+str(l)+'})\.|\.(?=D{1})|\.(?=.{'+str(l)+'}D{1})','D',my_map)
return re.sub('\.','S',my_map).split('\n')
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(finish_map(("D..", "...", "...")), (list, tuple)), "List or tuple"
assert list(finish_map(("D..XX.....",
"...X......",
".......X..",
".......X..",
"...X...X..",
"...XXXXX..",
"X.........",
"..X.......",
"..........",
"D...X....D"))) == ["DDSXXSDDDD",
"DDSXSSSSSD",
"DDSSSSSXSD",
"DDSSSSSXSD",
"DDSXSSSXSD",
"SSSXXXXXSD",
"XSSSSSSSSD",
"SSXSDDDDDD",
"DSSSSSDDDD",
"DDDSXSDDDD"], "Example"
assert list(finish_map(("........",
"........",
"X.X..X.X",
"........",
"...D....",
"........",
"X.X..X.X",
"........",
"........",))) == ["SSSSSSSS",
"SSSSSSSS",
"XSXSSXSX",
"SSSSSSSS",
"DDDDDDDD",
"SSSSSSSS",
'XSXSSXSX',
"SSSSSSSS",
"SSSSSSSS"], "Walls"
Aug. 29, 2014
Comments: