Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Pure heuristic solution in Creative category for Colder-Warmer by nicuveo
from functools import reduce, partial
from itertools import product
from math import hypot, sin
_a = lambda p: [x/len(p) for x in reduce(lambda a,b: map(sum,zip(a,b)),p)]
_b = lambda a,b,c,d,e: partial((_f1,_f2,_f3)[e+1],a,b,c,d)
_c = lambda s: [_b(a,b,c,d,e) for (a,b,_),(c,d,e) in zip(s,s[1:])]
_f1 = lambda a,b,c,d,p: (a-c)*(p[0]-c)+(b-d)*(p[1]-d) > 0
_f2 = lambda a,b,c,d,p: hypot(p[0]-a,p[1]-b) == hypot(p[0]-c,p[1]-d)
_f3 = lambda a,b,c,d,p: (c-a)*(p[0]-a)+(d-b)*(p[1]-b) > 0
_h = lambda a: lambda c: -hypot(c[0]-a[0],c[1]-a[1])
_k = lambda s,c,p: list(p) not in (x[:2] for x in s) and all(f(p) for f in c)
_n = lambda s,c: [p for p in product(range(10), repeat=2) if _k(s,c,p)]
checkio = lambda s: (lambda n: max(n,key=_h(_a(n))))(_n(s,_c(s)))
if __name__ == '__main__':
#This part is using only for self-checking and not necessary for auto-testing
from math import hypot
MAX_STEP = 12
def check_solution(func, goal, start):
prev_steps = [start]
for step in range(MAX_STEP):
row, col = func([s[:] for s in prev_steps])
if [row, col] == goal:
return True
if 10 <= row or 0 > row or 10 <= col or 0 > col:
print("You gave wrong coordinates.")
return False
prev_distance = hypot(prev_steps[-1][0] - goal[0], prev_steps[-1][1] - goal[1])
distance = hypot(row - goal[0], col - goal[1])
alteration = 0 if prev_distance == distance else (1 if prev_distance > distance else -1)
prev_steps.append([row, col, alteration])
print("Too many steps")
return False
assert check_solution(checkio, [7, 7], [5, 5, 0]), "1st example"
assert check_solution(checkio, [5, 6], [0, 0, 0]), "2nd example"
print("Done")
June 7, 2014
Comments: