CAPACITY_0 = 4
CAPACITY_1 = 7


class State:
    def __init__(self) -> None:
        self.jar_0 = 0
        self.jar_1 = 0

    def __str__(self) -> str:
        return f'Xarra 0: {self.jar_0} litros, Xarra 1: {self.jar_1} litros'


# Actions

def fill_0(state: State) -> State:
    "Encher xarra 0"
    state.jar_0 = CAPACITY_0
    return state


def fill_1(state: State) -> State:
    "Encher xarra 1"
    state.jar_1 = CAPACITY_1
    return state

def empty_0(state: State) -> State:
    "Vaciar xarra 0"
    state.jar_0 = 0
    return state


def empty_1(state: State) -> State:
    "Vaciar xarra 1"
    state.jar_1 = 0
    return state


def pour_0_to_1(state: State) -> State:
    "Verter xarra 0 en 1"
    space = CAPACITY_1 - state.jar_1
    exchange = state.jar_0 if state.jar_0 <= space else space
    state.jar_1 += exchange
    state.jar_0 -= exchange
    return state


def pour_1_to_0(state: State) -> State:
    "Verter xarra 1 en 0"
    space = CAPACITY_0 - state.jar_0
    exchange = state.jar_1 if state.jar_1 <= space else space
    state.jar_0 += exchange
    state.jar_1 -= exchange
    return state


def main() -> None:
    state = State()
    actions = (
        fill_1,
        pour_1_to_0,
        empty_0,
        pour_1_to_0,
        fill_1,
        pour_1_to_0,
    )
    for action in actions:
        print(action.__doc__)
        action(state)
        if check(state):
            print(f'Conseguido! {state}')
            break


def check(state: State) -> bool:
    return state.jar_0 == 6 or state.jar_1 == 6


if __name__ == '__main__':
    main()

@@ -55,24 +55,52 @@
 _init__(self
+, jar_0: int=0, jar_1: int=0
 ) -%3E None:%0A 
@@ -111,24 +111,25 @@
    self.
+_
 jar_0 = 
 0%0A      
@@ -116,24 +116,28 @@
 lf._jar_0 = 
+jar_
 0%0A        se
@@ -139,24 +139,25 @@
    self.
+_
 jar_1 = 
 0%0A%0A    d
@@ -148,17 +148,161 @@
 jar_1 = 
-0
+jar_1%0A%0A    @property%0A    def jar_0(self) -%3E int:%0A        return self._jar_0%0A%0A    @property%0A    def jar_1(self) -%3E int:%0A        return self._jar_1
 %0A%0A    de
@@ -479,49 +479,45 @@
     
-state.jar_0 = CAPACITY_0%0A    return state
+return State(CAPACITY_0, state.jar_1)
 %0A%0A%0Ad
@@ -567,32 +567,45 @@
 er xarra 1%22%0A    
+return State(
 state.jar_1 = CA
@@ -598,19 +598,18 @@
 ate.jar_
-1 =
+0,
  CAPACIT
@@ -611,33 +611,18 @@
 PACITY_1
+)
 %0A
-    return state
 %0A%0Adef em
@@ -680,40 +680,36 @@
     
-state.jar_0 = 0%0A    return state
+return State(0, state.jar_1)
 %0A%0A%0Ad
@@ -772,40 +772,36 @@
     
-state.jar_1 = 0%0A    return state
+return State(state.jar_0, 0)
 %0A%0A%0Ad
@@ -960,32 +960,45 @@
  else space%0A    
+return State(
 state.jar_1 += e
@@ -995,12 +995,11 @@
 jar_
-1 +=
+0 -
  exc
@@ -995,36 +995,33 @@
 jar_0 - exchange
-%0A   
+,
  state.jar_0 -= 
@@ -1019,12 +1019,11 @@
 jar_
-0 -=
+1 +
  exc
@@ -1027,33 +1027,17 @@
 exchange
-%0A    return state
+)
 %0A%0A%0Adef p
@@ -1196,24 +1196,37 @@
 e space%0A    
+return State(
 state.jar_0 
@@ -1226,17 +1226,16 @@
 .jar_0 +
-=
  exchang
@@ -1235,20 +1235,17 @@
 exchange
-%0A   
+,
  state.j
@@ -1250,17 +1250,16 @@
 .jar_1 -
-=
  exchang
@@ -1259,33 +1259,17 @@
 exchange
-%0A    return state
+)
 %0A%0A%0Adef m
@@ -1494,23 +1494,31 @@
 _doc__)%0A
-
        
+ state =
  action(

Convierte en inmutable