-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
29 lines (20 loc) · 766 Bytes
/
main.py
File metadata and controls
29 lines (20 loc) · 766 Bytes
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
# water jug solver
from collections import defaultdict
jug1, jug2, aim = 4, 3, 2
visited = defaultdict(lambda: False)
def wjs(amt1, amt2):
if (amt1 == aim and amt2 == 0) or (amt2 == aim and amt1 == 0):
print("( ", amt1, " , ", amt2, " )")
return True
if visited[(amt1, amt2)] == False:
print("( ", amt1, " , ", amt2, " )")
visited[(amt1, amt2)] = True
return (wjs(0, amt2) or
wjs(jug1, amt2) or
wjs(amt1, jug2) or
wjs(amt1 + min(amt2, (jug1 - amt1)), amt2 - min(amt2, (jug1 - amt1))) or
wjs(amt1 - min(amt1, (jug2 - amt2)), amt2 + min(amt1, (jug2 - amt2))))
else:
return False
print("STEPS :")
wjs(0, 0)