Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First - w/o recursion solution in Clear category for Find Enemy by freeman_lex
def find_enemy(you, dir, enemy):
dirs = ["N", "NE", "SE", "S", "SW", "NW"]
rot = dirs.index(dir)
dirs = dirs[rot:] + dirs[:rot]
curr = [dirs[0]] + [dirs[3]] + dirs[1:3] + dirs[4:]
x, y = ord(you[0]) - 65, int(you[1])
xe, ye = ord(enemy[0]) - 65, int(enemy[1])
deltas = {"N": ((0, -1),(0, -1)),
"NE": ((1, -1), (1, 0)),
"SE": ((1, 0), (1, 1)),
"S": ((0, 1), (0, 1)),
"SW": ((-1, 0), (-1, 1)),
"NW": ((-1, -1), (-1, 0))}
checked = {(x, y)}
edge = {d: {(x, y)} for d in "FBRL"}
distance = 1
while True:
for rel_dir, cells in edge.items():
next = set()
for x, y in cells:
for abs in curr:
dx, dy = deltas[abs][x % 2]
new = x + dx, y + dy
if new not in checked:
if new == (xe, ye):
return [rel_dir, distance]
next.add(new)
checked.add(new)
if distance == 1 and next:
if rel_dir in "FB" or len(next) == 2:
break
edge[rel_dir] = next
distance += 1
if __name__ == '__main__':
assert find_enemy('G5', 'N', 'G4') == ['F', 1], "N-1"
assert find_enemy('G5', 'N', 'I4') == ['R', 2], "NE-2"
assert find_enemy('G5', 'N', 'J6') == ['R', 3], "SE-3"
assert find_enemy('G5', 'N', 'G9') == ['B', 4], "S-4"
assert find_enemy('G5', 'N', 'B7') == ['L', 5], "SW-5"
assert find_enemy('G5', 'N', 'A2') == ['L', 6], "NW-6"
assert find_enemy('G3', 'NE', 'C5') == ['B', 4], "[watch your six!]"
assert find_enemy('H3', 'SW', 'E2') == ['R', 3], "right"
assert find_enemy('A4', 'S', 'M4') == ['L', 12], "true left"
print("You are good to go!")
Sept. 5, 2022