Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Set Objects solution in Clear category for Supply Line by kurosawa4434
from string import ascii_uppercase as au
from operator import or_
from functools import reduce
COL = 12
ROW = 9
def adjacent_hexes(hex_id):
col, row = au.index(hex_id[0]), int(hex_id[1])
diagonal_top = row + col % 2 - 1
diagonal_bottom = row + col % 2
adj_hexes = {
(col, row-1),
(col, row+1),
(col+1, diagonal_top),
(col+1, diagonal_bottom),
(col-1, diagonal_top),
(col-1, diagonal_bottom),
}
return set(map(lambda f: au[f[0]]+str(f[1]),
filter(lambda a: 0 <= a[0] < COL and 1 <= a[1] <= ROW, adj_hexes)))
def supply_line(you, depots, enemies):
obstacle_hexes = reduce(or_, map(adjacent_hexes, enemies), set()) | enemies
next_hexes = {you}
done_hexes = {you}
steps = 1
while next_hexes:
search_hexes = next_hexes
next_hexes = set()
for sx in search_hexes:
next_hexes |= adjacent_hexes(sx) - obstacle_hexes
if depots & next_hexes:
return steps
next_hexes -= done_hexes
done_hexes |= next_hexes
steps += 1
return None
if __name__ == '__main__':
assert supply_line("B4", {"F4"}, {"D4"}) == 6, 'simple'
assert supply_line("A3", {"A9", "F5", "G8"}, {"B3", "G6"}) == 11, 'multiple'
assert supply_line("C2", {"B9", "F6"}, {"B7", "E8", "E5", "H6"}) is None, 'None'
assert supply_line("E5", {"C2", "B7", "F8"}, set()) == 4, 'no enemies'
assert supply_line("A5", {"A2", "B9"}, {"B3", "B7", "E3", "E7"}) == 13, '2 depots'
print('"Run" is good. How is "Check"?')
July 6, 2018
Comments: