Example #1
0
def solve(current_state, rule_order):
    """
    Solve the puzzle with a list of rules
    """
    iteration = 1
    while not einstein.end_solution(current_state):
        # Safe guard to ensure a rules are being applied
        pre_rules_state = current_state
        print "######  begin iteration", iteration
        for rule in rule_order:
            old_state = current_state
            
            # See what changes state
            current_state = rule(current_state)
            if current_state != old_state: #something happened
                print 'RULE {0} changed state'.format(rule.__doc__)
            
            # see if any additional changes can be made    
            current_state = einstein.elimination_sweep(current_state)
                
        # Safe guard to ensure a rules are being applied 
        if current_state == pre_rules_state: #nothing happened
            break
        iteration += 1
    return current_state, iteration - 1
Example #2
0
def solve(current_state, rule_order):
    """
    Solve the puzzle with a list of rules
    """
    iteration = 1
    while not einstein.end_solution(current_state):
        # Safe guard to ensure a rules are being applied
        pre_rules_state = current_state
        print "######  begin iteration", iteration
        for rule in rule_order:
            old_state = current_state

            # See what changes state
            current_state = rule(current_state)
            if current_state != old_state:  #something happened
                print 'RULE {0} changed state'.format(rule.__doc__)

            # see if any additional changes can be made
            current_state = einstein.elimination_sweep(current_state)

        # Safe guard to ensure a rules are being applied
        if current_state == pre_rules_state:  #nothing happened
            break
        iteration += 1
    return current_state, iteration - 1
Example #3
0
    def test_elimination_sweep(self):
        """
        If a set of values dwindles to just one value then the last_man_standing rule applies.

        In this case officially assigning the value will correct state
        """
        new_state = einstein.deepcopy(einstein.START_STATE)
        new_state['1']['nationality'] = set(['norweigen'])
        new_state = einstein.elimination_sweep(new_state)
        self.assertNotIn('blue', new_state['2']['nationality'])
        self.assertNotIn('blue', new_state['3']['nationality'])
        self.assertNotIn('blue', new_state['4']['nationality'])
        self.assertNotIn('blue', new_state['5']['nationality'])
        ### LOCK
        self.assertEqual(einstein.get_position('norweigen', new_state), '1')
Example #4
0
    def test_elimination_sweep(self):
        """
        If a set of values dwindles to just one value then the last_man_standing rule applies.

        In this case officially assigning the value will correct state
        """
        new_state = einstein.deepcopy(einstein.START_STATE)
        new_state['1']['nationality'] = set(['norweigen'])
        new_state = einstein.elimination_sweep(new_state)
        self.assertNotIn('blue', new_state['2']['nationality'])
        self.assertNotIn('blue', new_state['3']['nationality'])
        self.assertNotIn('blue', new_state['4']['nationality'])
        self.assertNotIn('blue', new_state['5']['nationality'])
        ### LOCK
        self.assertEqual(einstein.get_position('norweigen', new_state), '1')