Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Lazy filtering generators solution in Clear category for Colder-Warmer by Leonix
import math, random
def length(a, b):
""" Euclidean distance. """
return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
def cmp(a, b):
""" 1, 0, or -1 when a > b, a == b, and a < b respectively. """
return (a > b) - (a < b)
def good_candidates(candidates, prev_step, step):
""" Filter candidates, only yielding those that satisfy given step result. """
for candidate in candidates:
if candidate != step[0:2] and candidate != prev_step[0:2]:
distance = length(candidate, step)
prev_distance = length(candidate, prev_step)
if cmp(prev_distance, distance) == step[2]:
yield candidate
def checkio(steps):
# All possible map cells in random (not quite... but random enough) order
randomizer = lambda k: random.random()
candidates = ([x, y] for x in sorted(range(10),key=randomizer) for y in sorted(range(10),key=randomizer))
# Apply several filters on `candidates`, filtering out everything
# that does not satisfy known conditions in `steps`.
step, steps = steps[0], steps[1:]
while len(steps) > 0:
prev_step, step, steps = step, steps[0], steps[1:]
candidates = good_candidates(candidates, prev_step, step)
# Suggest the first coordinate that satisfy all previous steps
return next(candidates)
Aug. 2, 2015
Comments: