Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
18-liner: bunker solution in Clear category for Bats Bunker by przemyslaw.daniel
def checkio(bunker):
from heapq import heappush, heappop
from itertools import product, chain
height, width, queue = len(bunker), len(bunker[0]), []
bats = {(x, y) for x, y in product(range(height), range(width)) if bunker[x][y] in 'AB'}
heappush(queue, (0.0, (0, 0), bats-{(0, 0)}))
while queue:
distance, (x, y), bats = heappop(queue)
if bunker[x][y] == 'A':
return distance
for a, b in bats:
vx, vy = (a-x)/(height*width), (b-y)/(height*width)
path, r = [(x+vx*i+0.5, y+vy*i+0.5) for i in range(height*width)], 0.1
path = [[(i+r, j+r), (i+r, j-r), (i-r, j+r), (i-r, j-r)] for i, j in path]
if any([bunker[int(c)][int(d)] == 'W' for c, d in chain(*path)]):
continue
d = __import__('math').hypot(x-a, y-b)
heappush(queue, (distance+d, (a, b), bats-{(a, b)}))
March 20, 2017
Comments: