Untitled
unknown
plain_text
2 years ago
1.4 kB
5
Indexable
def waterJugSolution(xCapacity, yCapacity, target, reverse=False):
path = [[0, 0]]
y = yCapacity
x = 0
path.append([x, y])
steps = 1
while ((y != target) and (x != target)):
temp = min(y, xCapacity - x)
x += temp
y -= temp
path.append([x, y])
steps += 1
if ((y == target) or (x == target)):
break
if y == 0:
y = yCapacity
path.append([x, y])
steps += 1
if x == xCapacity:
x = 0
path.append([x, y])
steps += 1
if reverse:
path = [[y, x] for x, y in path]
return [path, steps]
def solveGCD(a, b):
if b == 0:
return a
return solveGCD(b, a % b)
xCapacity = int(input("Jug 1 capacity: "))
yCapacity = int(input("Jug 2 capacity: "))
target = int(input("Target volume: "))
if target % solveGCD(xCapacity, yCapacity) == 0:
path1, step1 = waterJugSolution(xCapacity, yCapacity, target, reverse=False)
path2, step2 = waterJugSolution(yCapacity, xCapacity, target, reverse=True)
if step1 <= step2:
print("The path is:", path1)
print("The steps are:", step1)
else:
print("The path is:", path2)
print("The steps are:", step2)
else:
print("Solution failed")
Editor is loading...
Leave a Comment