Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second - with recursion solution in Clear category for Find Enemy by freeman_lex
def find_enemy(you, dir, enemy):
def look(data, d, c):
if result or c > 26:
return
next = set()
for x, y in data:
for abs in dic[d]:
dx, dy = deltas[abs][x % 2]
new = x + dx, y + dy
if new == (xe, ye):
result.append([d, c + 1])
return
next.add(new)
look(next, d, c + 1)
directions = ["N", "NE", "SE", "S", "SW", "NW"]
rot = directions.index(dir)
curr = directions[rot:] + directions[:rot]
dic = {
"F": curr[:2] + [curr[-1]],
"R": curr[1:3],
"B": curr[2:5],
"L": curr[4:6]}
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))}
result = []
for rel in "RLFB":
look([(x, y)], rel, 0)
return result[0]
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. 4, 2022
Comments: