-
Notifications
You must be signed in to change notification settings - Fork 0
/
B126_Day1.py
68 lines (65 loc) · 2.11 KB
/
B126_Day1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Vessel filling problem
# Calculate GCD to check validity of Diophantine equation
def GCD(a,b):
if b == 0:
return a;
return GCD(b,a%b)
# Function to define the steps.
# to - Vessel to be poured into
# fro - Vessel to be poured from
'''Algorithm :
A. Pouring from a ltr to b ltr vessel.
1. Fill the vessel of 'a' litres and empty into 'b' litres vessel.
2. Empty 'b' litres vessel whenever it becomes full. Fill 'a' litres vessel whenever it becomes empty.
3. Continue till one of the vessels achieve 'c' litres.
B. Pouring from b ltrs to a ltrs vessel.
1. Fill the vessel of 'b' litres and empty into 'a' litres vessel.
2. Empty 'a' litres vessel whenever it becomes full. Fill 'b' litres vessel whenever it becomes empty.
3. Continue till one of the vessels achieve 'c' litres.
'''
def vesselPour(to,fro,c):
# Initialize 'to' to 0 and 'fro' to current value
toVes = 0
froVes = fro
# Initialize step = 1
step = 1
# Condition
while (toVes is not c) and (froVes is not c):
pour = min(toVes,froVes)
# Update step
froVes = froVes - pour
toVes = toVes + pour
step = step + 1
# Check for breaking of condition
if (toVes == c) or (froVes == c):
break
# Else, if Vessel 1 is empty, fill it. If Vessel 2 is filled, then empty it.
if froVes == 0:
froVes = fro
step = step + 1
if toVes == to:
toVes = 0
step = step + 1
return step
# Function to calculate minimum no. of steps from among Case A and Case B.
def minSteps(a,b,c):
if b > a:
p = b
b = a
a = p
# Check validity of Diophantine equation
if (c%GCD(a,b) is not 0):
return -1
return (min(vesselPour(a,b,c),vesselPour(b,a,c)))
# Driver code
if __name__ == "__main__":
result = []
t = int(input())
while t > 0:
a = int(input())
b = int(input())
c = int(input())
result.append(minSteps(a,b,c))
t = t - 1
for r in result:
print(r)