Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Breadth-first search solution in Uncategorized category for Water Jars by MadHatter
def checkio(first, second, goal):
f = {(0, 0):[]}
while True:
for i, j in list(f.keys()):
if i == goal or j == goal: return f[(i, j)]
if (first, j) not in f: f[(first, j)] = f[(i, j)] + ['01']
if (i, second) not in f: f[(i, second)] = f[(i, j)] + ['02']
if (0, j) not in f: f[(0, j)] = f[(i, j)] + ['10']
if (i, 0) not in f: f[(i, 0)] = f[(i, j)] + ['20']
l = min(i, second - j)
if (i - l, j + l) not in f: f[(i - l, j + l)] = f[(i, j)] + ['12']
l = min(j, first - i)
if (i + l, j - l) not in f: f[(i + l, j - l)] = f[(i, j)] + ['21']
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
print(checkio(5, 7, 6)) # ['02', '21', '10', '21', '02', '21', '10', '21', '02', '21']
print(checkio(3, 4, 1)) # ["02", "21"]
Sept. 7, 2013