Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Min - Max solution in Clear category for Colder-Warmer by JamesArruda
# migrated from python 2.7
from itertools import product
from collections import Counter
COMPARES = {1: lambda x,y:y > x,0:lambda x,y:x == y,-1:lambda x,y:y < x}
def get_dist(p1,p2):
# Skip the square root, since it's a monotonic transform
return (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2
def get_feasible(currp,prevp,availpts,dir):
# get the initial distances
prevdists = {pt:get_dist(prevp,pt) for pt in availpts}
currdists = {pt:get_dist(currp,pt) for pt in availpts}
feaspts = [pt for pt in availpts if COMPARES[dir](currdists[pt],prevdists[pt])]
return feaspts
def get_groups(currp,prevp,availpts):
prevdists = {pt:get_dist(prevp,pt) for pt in availpts}
currdists = {pt:get_dist(currp,pt) for pt in availpts}
ct = Counter()
for pt in availpts:
dd = currdists[pt]-prevdists[pt]
if dd > 0:
ct[1] += 1
elif dd == 0:
ct[0] += 1
else:
ct[-1] += 1
return ct
def minmaxproc(prevp,availpts):
minmax = float('inf')
minmaxpt = None
for pt in availpts:
grps = get_groups(pt,prevp,availpts)
nmax = grps.most_common(1)[0][1]
if nmax < minmax:
minmax = nmax
minmaxpt = list(pt)
return minmaxpt
def checkio(steps):
# Another min-max approach!
# We need to find the coordinate that splits how warm/cold we are up to give
# more information.
allpts = [(x,y) for x,y in product(list(range(10)),list(range(10)))]
# remove the first point
allpts.remove(tuple(steps[0][:2]))
if len(steps) == 1:
return minmaxproc(steps[0][:2],allpts)
else:
for i in range(1,len(steps)):
currp = steps[i][:2]
prevp = steps[i-1][:2]
allpts.remove(tuple(currp))
dir = steps[i][2]
allpts = get_feasible(currp,prevp,allpts,dir)
return minmaxproc(currp,allpts)
return [0, 0]
March 4, 2015
Comments: