Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Water Jars by Moff
def calc_state(first_volume, second_volume, operations):
first = second = 0
actions = {
"01": lambda f, s: (first_volume, s),
"02": lambda f, s: (f, second_volume),
"12": lambda f, s: (
f - (second_volume - s if f > second_volume - s else f),
second_volume if f > second_volume - s else s + f),
"21": lambda f, s: (
first_volume if s > first_volume - f else s + f,
s - (first_volume - f if s > first_volume - f else s)),
"10": lambda f, s: (0, s),
"20": lambda f, s: (f, 0)}
for act in operations:
first, second = actions[act](first, second)
return first, second
def checkio(first_volume, second_volume, goal):
"""bfs"""
visited = set()
queue = [[]]
while queue:
x = queue.pop()
state = calc_state(first_volume, second_volume, x)
if any(v == goal for v in state):
return x
if state not in visited:
visited.add(state)
for act in ('01', '02', '10', '20', '12', '21'):
queue.insert(0, x + [act])
Aug. 31, 2015