Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Enemy by mortonfox
def step(cell, direction):
if direction == 'N':
return cell[0] + chr(ord(cell[1]) - 1)
if direction == 'S':
return cell[0] + chr(ord(cell[1]) + 1)
if direction == 'NE':
return chr(ord(cell[0]) + 1) + chr(ord(cell[1]) - ord(cell[0]) % 2)
if direction == 'NW':
return chr(ord(cell[0]) - 1) + chr(ord(cell[1]) - ord(cell[0]) % 2)
if direction == 'SE':
return chr(ord(cell[0]) + 1) + chr(ord(cell[1]) + 1 - ord(cell[0]) % 2)
if direction == 'SW':
return chr(ord(cell[0]) - 1) + chr(ord(cell[1]) + 1 - ord(cell[0]) % 2)
def fringe(cell, facing, step_count):
dirs = ('N', 'NE', 'SE', 'S', 'SW', 'NW')
findx = dirs.index(facing)
ndirs = dirs[findx:] + dirs[:findx]
# forward
c = cell
for _i in range(step_count):
c = step(c, ndirs[0])
forwards = [c]
c1 = c
c2 = c
for _i in range(step_count - 1):
c1 = step(c1, ndirs[2])
forwards.append(c1)
c2 = step(c2, ndirs[4])
forwards.append(c2)
# back
c = cell
for _i in range(step_count):
c = step(c, ndirs[3])
backs = [c]
c1 = c
c2 = c
for _i in range(step_count - 1):
c1 = step(c1, ndirs[1])
backs.append(c1)
c2 = step(c2, ndirs[5])
backs.append(c2)
# left
c = cell
for _i in range(step_count):
c = step(c, ndirs[5])
lefts = [c]
for _i in range(step_count):
c = step(c, ndirs[3])
lefts.append(c)
# right
c = cell
for _i in range(step_count):
c = step(c, ndirs[1])
rights = [c]
for _i in range(step_count):
c = step(c, ndirs[3])
rights.append(c)
return forwards, backs, lefts, rights
def find_enemy(you, direction, enemy):
step_count = 0
while True:
step_count += 1
forwards, backs, lefts, rights = fringe(you, direction, step_count)
if enemy in forwards:
return ['F', step_count]
if enemy in backs:
return ['B', step_count]
if enemy in lefts:
return ['L', step_count]
if enemy in rights:
return ['R', step_count]
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!")
March 2, 2017
Comments: