Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Colder-Warmer by macfreek
#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
from itertools import product
from random import choice
def cmp(x, y):
"""Revival of Python 2's cmp() function.
Returns -1 for xy."""
return int(x > y) - int(x < y)
def checkio(steps):
"""brute force check of all possible coords"""
# print("------------")
# for s in steps:
# print(s)
if len(steps) == 1:
return (4,4)
# find possible solutions
nsteps = len(steps)
def is_candidate(x,y):
sx, sy, status = steps[0]
if sx == x and sy == y:
return False
dist_prev = (x - sx)**2 + (y - sy)**2
for i in range(1, nsteps):
sx, sy, status = steps[i]
if sx == x and sy == y:
return False
dist = (x - sx)**2 + (y - sy)**2
if cmp(dist_prev, dist) != status:
return False
dist_prev = dist
return True
candidates = []
for x, y in product(range(10), repeat=2):
if is_candidate(x,y):
candidates.append((x,y))
# print(len(candidates))
return choice(candidates) # random choice
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"
May 3, 2014