Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Chapter2/missionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def __str__(self) -> str:
"The boat is on the {} bank.")\
.format(self.wm, self.wc, self.em, self.ec, ("west" if self.boat else "east"))

def __eq__(self, other) -> bool:
return (self.wm == other.wm) and (self.wc == other.wc) and \
(self.em == other.em) and (self.ec == other.ec) and \
(self.boat == other.boat)

def __hash__(self) -> int:
state: int = self.wm * (MAX_NUM + 1)**3 + self.wc * (MAX_NUM + 1)**2 + self.em * (MAX_NUM + 1) + self.ec
state *= 1 if self.boat else -1
return hash(state)

def goal_test(self) -> bool:
return self.is_legal and self.em == MAX_NUM and self.ec == MAX_NUM

Expand Down