Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Bats Bunker by TovarischZhukov
def inters(a,b,c,d):
comm=(b[0]-a[0])*(d[1]-c[1])-(b[1]-a[1])*(d[0]-c[0])
if not comm: return True
r1=(a[1]-c[1])*(d[0]-c[0])-(a[0]-c[0])*(d[1]-c[1])
r2=(a[1]-c[1])*(b[0]-a[0])-(a[0]-c[0])*(b[1]-a[1])
r=r1/float(comm)
s=r2/float(comm)
return not(0<=r<=1 and 0<=s<=1)
def check(i, j, pos1, pos2):
pos1=(pos1[0]+0.5, pos1[1]+0.5)
pos2=(pos2[0]+0.5, pos2[1]+0.5)
a=inters(pos1, pos2, (0+i, 0+j), (1+i, 0+j))
b=inters(pos1, pos2, (0+i, 0+j), (0+i, 1+j))
c=inters(pos1, pos2, (0+i, 1+j), (1+i, 1+j))
d=inters(pos1, pos2, (1+i, 1+j), (1+i, 0+j))
return a*b*c*d
def find(data, walls, bats, current, alph, path):
retval=[]
bats=list(bats)
bats.remove(current)
flag=True
for wall in walls:
if not check(wall[0],wall[1],current,alph): flag=False
if flag: return path+((current[0]-alph[0])**2+(current[1]-alph[1])**2)**.5
for bat in bats:
flag = True
for wall in walls:
if not check(wall[0], wall[1], current, bat): flag=False
if flag:
ln=((current[0]-bat[0])**2+(current[1]-bat[1])**2)**.5
r=find(data, walls, bats, bat, alph, path+ln)
if r: retval.append(r)
return min(retval) if retval else 0
def checkio(data):
walls=set()
bats=list()
for i,el in enumerate(data):
for j, val in enumerate(el):
if val=="W":walls.add((i,j))
elif val =="B": bats.append((i,j))
elif val =="A": alph=(i,j)
return round(find(data, walls, bats, bats[0], alph, 0),2) if bats else 0
Feb. 26, 2016