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 - b)**2 + (a - b)**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:
# 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, steps[1:]
while len(steps) > 0:
prev_step, step, steps = step, steps, steps[1:]
candidates = good_candidates(candidates, prev_step, step)
# Suggest the first coordinate that satisfy all previous steps
Aug. 2, 2015