From 98d5c95973a041496e8b2385e1372d0423bd2923 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Wed, 29 Apr 2015 06:57:52 +0200 Subject: [PATCH] Wolfsheep improved --- wolfsheep.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/wolfsheep.py b/wolfsheep.py index 8596e5c..04f4705 100644 --- a/wolfsheep.py +++ b/wolfsheep.py @@ -17,8 +17,9 @@ class Model: return ",".join(self.left) + " <-> " + ",".join(self.right) class Simulator: + invalid_states = [ set({Model.Wolf,Model.Sheep}), set({Model.Sheep, Model.Cabbage}) ] + def simulate(self, state, stack=[]): - #stack.append(state) stack = stack + [state] if state.right == set([Model.Man,Model.Wolf,Model.Sheep,Model.Cabbage]): return stack @@ -32,12 +33,11 @@ class Simulator: nextStates.append( Model(state.left | set([Model.Man, obj]) , state.right - set([Model.Man,obj]) ) ) nextStates = list(filter( - lambda s: s not in stack and not( - ( (set([Model.Wolf,Model.Sheep]) <= s.left or set([Model.Sheep, Model.Cabbage]) <= s.left) and Model.Man not in s.left ) or - ( (set([Model.Wolf,Model.Sheep]) <= s.right or set([Model.Sheep, Model.Cabbage]) <= s.right) and Model.Man not in s.right ) + lambda s: not( + s in stack or + any([ any([ invalid_state <= m for invalid_state in Simulator.invalid_states ]) and Model.Man not in m for m in [s.left,s.right] ]) ) , nextStates)) #Remove invalid and previous states. - #print("%s: %s\nstack: %s" % (state,nextStates,stack)) for s in nextStates: m = self.simulate(s,stack)