Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Safe Coasts by rafal.pawlowski
import numpy
from itertools import chain
def finish_map(map):
l=[list(el) for el in map]
X=len(l)-1
Y=len(l[0])-1
indexy=[]
for i in range(X+1):
for j in range(Y+1):
if l[i][j]=='D':
indexy.append((i,j))
print(l)
sasiady = lambda x, y : [(x2, y2) for x2 in range(x-1, x+2)
for y2 in range(y-1, y+2)
if (-1 < x <= X and
-1 < y <= Y and
((x == x2 and y != y2) or (x != x2 and y == y2)) and
(0 <= x2 <= X) and
(0 <= y2 <= Y))]
potentially_danger = lambda x, y : [(x2, y2) for x2 in range(x-1, x+2)
for y2 in range(y-1, y+2)
if (-1 < x <= X and
-1 < y <= Y and
(x != x2 or y != y2) and
(0 <= x2 <= X) and
(0 <= y2 <= Y))]+[(x,y)]
print(X,Y)
for i in range(X+1):
for j in range(Y+1):
if all(el in ['P','D','.'] for el in [map[m][l] for m,l in potentially_danger(i,j)]):
l[i][j]='P'
print(numpy.array(l))
def wyspy(lista,poz): # zasieg D
visited=[]
result=[]
lst=[]
i=poz[0]
j=poz[1]
if lista[i][j]=='P':
lst.append((i,j))
visited.append((i,j))
result.append((i,j))
print(lst)
while lst:
pom=[]
sas=[]
for el in lst:
sas.append(sasiady(el[0],el[1]))
sas = list(chain(*sas))
print(sas)
for nei in sas: # nie wspolrzedne
if lista[nei[0]][nei[1]] == 'P' and nei not in visited: # ale wartosc
visited.append(nei)
pom.append(nei)
result.append(nei)
lst=pom[:]
return result
gdzie_straszy=[]
for el in indexy:
gdzie_straszy.append(wyspy(l,el))
print(gdzie_straszy)
gdzie_straszy=list(chain(*gdzie_straszy))
for i in range(len(l)):
for j in range(len(l[0])):
if (i,j) in gdzie_straszy:
l[i][j]='D'
elif l[i][j]=='P' or l[i][j]=='.':
l[i][j]='S'
p=[''.join(row) for row in l]
return p
print(finish_map(("........","........","X.X..X.X","........","...D....","........","X.X..X.X","........","........")))
Dec. 12, 2017