• + 0 comments

    My Python3 solution

    def solve(a, b, c):
        yes, no = "YES", "NO"
        a, b = max(a, b), min(a, b)
    
        b_states = {0, b}
        a_states = {0, a}
        b_state = b_states.copy()
        a_state = a_states.copy()
        if c in a_states or c in b_states:
            return yes
        search = True
        while search:
            for jug_a in a_state:
                for jug_b in b_state:
                    
                    a1, a2 = min(a, jug_a + jug_b), max(0, jug_a - (b - jug_b))
                    b1, b2 = min(b, jug_b+jug_a), max(0, jug_b - (a - jug_a))
                    
                    if c in {a1, a2, b1, b2}:
                        return yes
            
                    a_states.add(a1)
                    a_states.add(a2)
                    
                    b_states.add(b1)
                    b_states.add(b2)
        
            if a_states == a_state and b_states == b_state:
                break
        
    
            b_state = b_states.copy()
            a_state = a_states.copy()
    
        # print(a_states, b_states)
        return no