Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
21-liner: piece of cake solution in Clear category for Supply Line by przemyslaw.daniel
def get_neighbors(pos):
x, y = map(ord, pos)
k = (-1)**(x % 2)
neighbors = [(0, -1), (0, 1), (1, 0), (-1, 0), (-1, k), (1, k)]
return [chr(x + i) + chr(y + j) for i, j in neighbors]
def supply_line(start, depots, enemies):
from heapq import heapify, heappush, heappop
valid = {x+y for x in 'ABCDEFGHIJKL' for y in '123456789'}
valid -= set(sum([get_neighbors(x) for x in enemies], []))
queue, visited = [(1, x) for x in get_neighbors(start)], set()
heapify(queue)
while queue:
length, position = heappop(queue)
if position not in valid or position in visited:
continue
if position in depots:
return length
visited |= {position}
for pos in get_neighbors(position):
heappush(queue, (length+1, pos))
Aug. 29, 2018
Comments: