Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Cubic coordinates solution in Clear category for Find Enemy by martin_b
def find_enemy(robot, orientation, enemy):
# distance and rotation computed in cubic coordinates
# from the great http://www.redblobgames.com/grids/hexagons/
def cube(col, row, oddq):
z = row - (col + (col & 1) * oddq) // 2
return (col, z, -col - z)
def rotation(c, n):
while n > 0:
c, n = (-c[2], -c[0], -c[1]), n - 1
return c
def trisector(a):
return (a > 0) - (a < 0)
rx = ord(robot[0])
dx, dy = (ord(enemy[0]) - rx, int(robot[1]) - int(enemy[1]))
c = cube(dx, dy, 1 if (rx & 1) else -1)
distance = sum(map(abs, c)) // 2
orientations = {'N': 0, 'NE': 5, 'SE': 4, 'S': 3, 'SW': 2, 'NW': 1}
(cx, cy, cz) = rotation(c, orientations[orientation])
direction = set({1: "FRB", 0: "FB", -1: "BLF"}[trisector(cx)])
direction &= set({1: "LF", 0: "RL", -1: "RB"}[trisector(cy)])
direction &= set({1: "BL", 0: "RL", -1: "FR"}[trisector(cz)])
return [direction.pop(), distance]
Jan. 14, 2017