Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
BFS solution in Clear category for Supply Line by tom-tom
def supply_line(you, depots, enemies, last_row='9', last_column='L'):
"""The minimum number of steps from you to the supply depot, avoiding enemies"""
def neighbours(land):
column, row = map(ord, land)
vshift = column % 2
return {chr(x) + chr(y) for x, y in (
(column, row - 1), (column, row + 1),
(column - 1, row - vshift), (column - 1, row + 1 - vshift),
(column + 1, row - vshift), (column + 1, row + 1 - vshift))
if 'A' <= chr(x) <= last_column and
'1' <= chr(y) <= last_row}
old_and_prohibited, current = enemies.union(*map(neighbours, enemies)), {you}
steps = 0
while current:
steps += 1
new = set.union(*map(neighbours, current)) - old_and_prohibited
if new & depots:
return steps
old_and_prohibited, current = old_and_prohibited | current, new
Jan. 10, 2019