-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.py
More file actions
34 lines (25 loc) · 1.09 KB
/
operator.py
File metadata and controls
34 lines (25 loc) · 1.09 KB
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
from state import Jug
class Operator:
def __init__(self, from_, to):
self.from_ = from_
self.to = to
def precondition_fulfilled(self, jug):
return jug.contents[self.from_] != 0 and jug.contents[self.to] != Jug.CAPACITIES[self.to]
def use(self, jug):
result = Jug()
volume = min(jug.contents[self.from_], Jug.CAPACITIES[self.to] - jug.contents[self.to])
index_untouched = next(filter(lambda i: i not in (self.from_, self.to), Jug.INDICES))
result.contents[self.from_] = jug.contents[self.from_] - volume
result.contents[self.to] = jug.contents[self.to] + volume
result.contents[index_untouched] = jug.contents[index_untouched]
return result
def __str__(self):
return f"Operator [From={self.from_}, To={self.to}]"
if __name__ == "__main__":
o1 = Operator(from_= 0, to= 1)
print(o1)
jug = Jug([2, 0, 3])
print("-------------")
print("Jug: ", jug)
print("Precondition: ", o1.precondition_fulfilled(jug))
print("After: ", o1.use(jug))