Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Not so effective but it works solution in Clear category for Fortress Cannons by Oleg_Domokeev
from itertools import product
dirs = ['N', 'NW', 'SW', 'S', 'SE', 'NE']
def fortress_cannons(fort, cannons, enemies):
coords = lambda point: (ord(point[0]) - 64, int(point[1]))
a, b = coords(fort)
def enemy(pos):
(x, y), sectors = coords(pos), []
down, up = b + (x + 1) // 2 - (a + 1) // 2, b - x // 2 + a // 2
down30, up30 = down + (x - a), up - (x - a)
if x == a:
dist = abs(y - b)
if y < b:
sectors.extend([('N', 0), ('N', 60), ('N', 120)])
else:
sectors.extend([('S', 0), ('S', 60), ('S', 120)])
elif x < a:
if y < down:
dist = (a - x) + (down - y)
elif y > up:
dist = (a - x) + (y - up)
else:
dist = a - x
if y == down:
sectors.append(('NW', 0))
if y == up:
sectors.append(('SW', 0))
else:
if y < up:
dist = (x - a) + (up - y)
elif y > down:
dist = (x - a) + (y - down)
else:
dist = x - a
if y == up:
sectors.append(('NE', 0))
if y == down:
sectors.append(('SE', 0))
if x < a:
if y <= down30:
sectors.append(('N', 60))
if y <= down:
sectors.append(('N', 120))
if down30 <= y <= b - (a % 2) * ((x +1) % 2):
sectors.append(('NW', 60))
if y <= up:
sectors.append(('NW', 120))
if b + ((a + 1) % 2) * (x % 2) <= y <= up30:
sectors.append(('SW', 60))
if down <= y:
sectors.append(('SW', 120))
if up30 <= y:
sectors.append(('S', 60))
if up <= y:
sectors.append(('S', 120))
if x > a:
if y <= up30:
sectors.append(('N', 60))
if y <= up:
sectors.append(('N', 120))
if up30 <= y <= b - (a % 2) * ((x +1) % 2):
sectors.append(('NE', 60))
if y <= down:
sectors.append(('NE', 120))
if b + ((a + 1) % 2) * (x % 2) <= y <= down30:
sectors.append(('SE', 60))
if up <= y:
sectors.append(('SE', 120))
if down30 <= y:
sectors.append(('S', 60))
if down <= y:
sectors.append(('S', 120))
return (dist, sectors)
feinde = []
for feind in enemies:
feinde.append(enemy(feind))
possible_dist = set([i for cannon in cannons for i in range(cannon[1], cannon[2] + 1)])
if any(feind[0] not in possible_dist for feind in feinde):
return
variants = product(dirs, repeat = len(cannons))
for variant in variants:
tests = [False] * len(feinde)
for i, cannon in enumerate(cannons):
for j, feind in enumerate(feinde):
if ((variant[i], cannon[0]) in feind[1]) and (cannon[1] <= feind[0] <= cannon[2]):
tests[j] = True
if all(test for test in tests):
return list(variant)
Jan. 22, 2019