Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
26-liner: clean solution in Clear category for Fortress Cannons by przemyslaw.daniel
def calc_distance(a, b):
y0, y1 = ord(a[0])-65, ord(b[0])-65
x0, x1 = int(a[1])-y0 // 2-1, int(b[1])-y1 // 2-1
return max(abs(x1-x0), abs(y1-y0), abs(x1-x0+y1-y0))
def calc_angle(a, b):
from math import pi, acos
x0, y0, x1, y1 = map(ord, a+b)
y0, y1 = y0-(x0 % 2)/2, y1-(x1 % 2)/2
x0, x1 = 3**0.5/2*x0, 3**0.5/2*x1
c = ((x0-x1)**2+(y0-y1)**2)**0.5
result = acos((x0-x1)/c)*[-1, 1][y0 > y1]
return 180+round(result/pi*180)
def fortress_cannons(fort, cannons, enemies):
from itertools import product
directions = 'SE S SW NW N NE'.split()
for config in product(directions, repeat=len(cannons)):
hit = set()
for (arc, min_dist, max_dist), direct in zip(cannons, config):
angle = directions.index(direct)*60+30
hit |= {enemy for enemy in enemies if
min_dist <= calc_distance(fort, enemy) <= max_dist and
angle <= calc_angle(fort, enemy)+arc/2 <= angle+arc}
if len(hit) == len(enemies):
return config
Jan. 22, 2019
Comments: