Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Enemy by Sim0000
DIR = {'N':0, 'NE':2, 'E':3, 'SE':4, 'S':6, 'SW':8, 'W':9, 'NW':10}
RDIR = {0:'F',1:'F',2:'R',3:'R',4:'R',5:'B',6:'B',7:'B',8:'L',9:'L',10:'L',11:'F'}
def find_enemy(you, dir, enemy):
# A : 0, B : 1,...
pos = lambda p: (ord(p[0]) - 65, int(p[1]))
x0, y0 = pos(you)
x1, y1 = pos(enemy)
dx = abs(x1 - x0)
# d is direction to enemy
# N
# NW | NE
# \ b 0 1 /
# a 2
# W-9 3-E
# 8 4
# / 7 6 5 \
# SW | SE
# S
if dx == 0:
d = 0 if y1 < y0 else 6
distance = dx + abs(y1 - y0)
else:
# d can be determined by where is y1. if y1 < v0, d = 1
# NE
# + v0
# /|
# / |
# / |
# / |
# +----+---- y0
# x0\ |x1
# \ |
# \ |
# \|
# + v1
# SE
v0 = y0 - (dx + 1 - x0 % 2) // 2
v1 = y0 + (dx + x0 % 2) // 2
d = 1 if y1 < v0 else 2 if y1 == v0 else 3 if y1 < v1 else 4 if y1 == v1 else 5
if x1 < x0: d = 12 - d # mirror
distance = dx + (0 if v0 <= y1 <= v1 else v0 - y1 if y1 < v0 else y1 - v1)
return [RDIR[(d - DIR[dir]) % 12], distance]
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!")
Nov. 22, 2016
Comments: