Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
unreadable math solution in Clear category for Find Enemy by Leonix
import string, math
def find_enemy(me, facing, enemy):
""" Represent hexes as complex numbers of their center.
We take each hex on the grid to be of height 1.
This gives us A1=0; A2=-1j; B1=(sqrt(0.75), -0.5j)
Convert enemy coordinate to frame of reference where "me" is at (0,0)
and facing north, then analyze direction and distance
in that simple frame of reference. """
enemy = (complex_from_hex(enemy) - complex_from_hex(me)) / direction_from_hex(facing) * direction_from_hex('N')
return [direction(enemy), manhattan_dist(enemy)]
def complex_from_hex(c):
""" Complex number representing center of given hex. """
x = string.ascii_uppercase.index(c[0])
y = 1.0-int(c[1])
if x % 2 == 1:
y -= .5
return x*math.sqrt(.75) + y*1j
def direction_from_hex(facing):
""" Complex number of len=1 facing given direction. N is 1j, S is -1j. """
deg60 = .5 + 1j*math.sqrt(.75)
return 1j * (deg60**['N','NW','SW','S','SE','NE'].index(facing))
def direction(c):
""" Direction relative to 0,0 and facing north. """
if is_in_front(c):
return 'F'
if is_in_front(-c):
return 'B'
if c.real > 0:
return 'R'
return 'L'
def is_in_front(c):
return c.imag > 0 and abs(c.real) <= math.sqrt(3) * (c.imag-0.5)
def manhattan_dist(c):
h = abs(c.real*math.sqrt(4/3))
v = max(0, abs(c.imag) - h/2)
return round(h+v)
June 18, 2019