Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Water Jars by gyahun_dash
from itertools import chain
def checkio(first, second, goal):
jars = first, second
if goal in jars:
return ['0{}'.format(jars.index(goal) + 1)]
small, large = min(jars), max(jars)
indices = [jars.index(j) + 1 for j in (small, large)]
states = [large] + [(small * i) % large for i in range(1, large + 1)]
revers = states[::-1]
left, right = [s.index(goal) for s in (states, revers)]
supply = lambda act, volumes: (act if v < small else '' for v in volumes)
if left <= right:
routines = ['0{0} {0}{1}'.format(*indices)] * left
aux = '{1}0 {0}{1}'.format(*indices)
tasks = zip(supply(aux, states[: left]), routines)
else:
routines = [''] + ['{0}0 {1}{0}'.format(*indices)] * (right - 1)
aux = '0{1} {1}{0}'.format(*indices)
tasks = zip(routines, supply(aux, revers[: right]))
return ' '.join(chain.from_iterable(tasks)).split()
May 24, 2014
Comments: