-
Notifications
You must be signed in to change notification settings - Fork 0
/
3 waterjug problem bruteforce.py
78 lines (69 loc) · 1.68 KB
/
3 waterjug problem bruteforce.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
69
70
71
72
73
74
75
76
77
78
from itertools import combinations, permutations
from random import randint
check = False
def fill(s,idx):
capacity=[12,8,5]
if(s[idx]<capacity[idx]):
check=True
s[idx]=capacity[idx]#fill first
return s,check
def empty(s,idx):
capacity=[12,8,5]
if(s[idx]>0):
check=True
s[idx]=0#fill first
return s,check
def transfer(s,t,f):
check=True
capacity=[12,8,5]
x=min(s[f],abs(capacity[t]-s[t]))
if(x==0):
check=False
s[f]-=x
s[t]+=x
return s,check
def solve(s,g):
capacity=[12,8,5]
to=0
_from=1
y=-1
yin=-1
c=permutations([0,1,2],2)
c=list(c)
counter=0
c1=0
print(s)
while(1):
counter+=1
comb=c[counter%6]
to,_from=comb
if(s==g):
print('suceess, goal achieved',c1)
break
y=randint(1,4)
if(y!=yin):
if(y==1 and s[to]==0):
s,check=fill(s,to)
if(check):
print(s)
c1+=1
if(y==2 and s[to]==capacity[to]):
s,check=empty(s,to)
if(check):
print(s)
c1+=1
if(y==3):
s,check=transfer(s,to,_from)
if(check):
print(s)
c1+=1
if(y==4):
s,check=transfer(s,_from,to)
if(check):
print(s)
c1+=1
yin=y
if __name__=="__main__":
s=[12,0,0]
g=[6,0,0]
solve(s,g)