Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
For PositronicLlama solution in Clear category for Water Jars by veky
def checkio(first, second, goal):
"""Shortest move sequence (assuming exists) achieving goal in one jar."""
lake = object()
def path(main, aux):
"""One (of two) candidates for a solution"""
solution = []
def pour(*fromto):
"""Register a move from one jar to another"""
ids = map([lake, first, second].index, fromto)
solution.append("".join(map(str, ids)))
if goal == aux: # degenerate case, goal in aux jar
pour(lake, aux)
else:
total = 0 # enough, since one of them is always full or empty
while total not in (goal, goal + main):
if total < main: # fill aux jar
total += aux
pour(lake, aux)
else: # empty main jar
total -= main
pour(main, lake)
pour(aux, main)
return solution
candidates = [path(main=first, aux=second), path(main=second, aux=first)]
return min(candidates, key=len)
assert checkio(5, 7, 6) == "02 21 10 21 02 21 10 21 02 21".split()
assert checkio(3, 4, 1) == "02 21".split()
Aug. 25, 2013
Comments: