Example #1
0
    def test_next_to_houses(self):
        """
        Some rules tell you that next to a house a certain condition is true.

        The white house is to the left of the green house.
        House 2 is to the right of house 1.
        1, 2, 3, 4, 5
        """
        self.assertIsNone(einstein.left_of('1'))
        self.assertIsNone(einstein.right_of('5'))
        self.assertIsNone(einstein.left_of('20'))
        self.assertEqual('1', einstein.left_of('2'))
        self.assertEqual('3', einstein.left_of('4'))
        self.assertEqual('3', einstein.right_of('2'))
        self.assertEqual(['2', '4'], einstein.next_to('3'))
        self.assertEqual(['2'], einstein.next_to('1'))
        self.assertEqual(['4'], einstein.next_to('5'))
Example #2
0
    def test_next_to_houses(self):
        """
        Some rules tell you that next to a house a certain condition is true.

        The white house is to the left of the green house.
        House 2 is to the right of house 1.
        1, 2, 3, 4, 5
        """
        self.assertIsNone(einstein.left_of('1'))
        self.assertIsNone(einstein.right_of('5'))
        self.assertIsNone(einstein.left_of('20'))
        self.assertEqual('1', einstein.left_of('2'))
        self.assertEqual('3', einstein.left_of('4'))
        self.assertEqual('3', einstein.right_of('2'))
        self.assertEqual(['2', '4'], einstein.next_to('3'))
        self.assertEqual(['2'], einstein.next_to('1'))
        self.assertEqual(['4'], einstein.next_to('5'))
Example #3
0
def rule4(state):
    """The green house is on the left of the white house."""
    green_house = einstein.get_position('green', state)
    white_house = einstein.get_position('white', state)
    # Green and white are already defined
    if green_house and white_house:
        return state
    # Green is defined so we can define white
    elif green_house:
        return einstein.assign_value(einstein.right_of(house), 'house_color',
                                     'white', state)
    # White is defined so we can define green
    elif white_house:
        return einstein.assign_value(einstein.right_of(house), 'house_color',
                                     'white', state)
    # Neither Green or White are defined
    else:
        # Go through all houses and make the following assertions
        for house in state.keys():
            house_to_right = einstein.right_of(house)
            house_to_left = einstein.left_of(house)
            # Nothing on the left so this cannot be the white house
            if not house_to_left:
                state = einstein.remove_value(house, 'house_color', 'white',
                                              state)
            # nothing o the right so this cannot be the green house
            elif not house_to_right:
                state = einstein.remove_value(house, 'house_color', 'green',
                                              state)
            if house_to_right:
                has_green = 'green' in state[house]['house_color']
                white_to_right = 'white' in state[house_to_right][
                    'house_color']
                # If this house_color is green
                # BUT the house on the right is not white
                # THEN green must be removed from this house
                if has_green and not white_to_right:
                    state = einstein.remove_value(house, 'house_color',
                                                  'green', state)
                # If this house is NOT green
                # THEN the house to the right cannot be white
                elif not has_green and white_to_right:
                    state = einstein.remove_value(house_to_right,
                                                  'house_color', 'white',
                                                  state)
        return state
Example #4
0
def rule4(state):
    """The green house is on the left of the white house."""
    green_house = einstein.get_position('green', state)
    white_house = einstein.get_position('white', state)
    # Green and white are already defined
    if green_house and white_house:
        return state
    # Green is defined so we can define white
    elif green_house:
        return einstein.assign_value(einstein.right_of(house), 'house_color', 'white', state)
    # White is defined so we can define green
    elif white_house:
        return einstein.assign_value(einstein.right_of(house), 'house_color', 'white', state)
    # Neither Green or White are defined
    else:
        # Go through all houses and make the following assertions
        for house in state.keys():
            house_to_right = einstein.right_of(house)
            house_to_left = einstein.left_of(house)
            # Nothing on the left so this cannot be the white house
            if not house_to_left:
               state = einstein.remove_value(house, 'house_color', 'white', state)
            # nothing o the right so this cannot be the green house
            elif not house_to_right:
                state = einstein.remove_value(house, 'house_color', 'green', state)
            if house_to_right:
                has_green = 'green' in state[house]['house_color']
                white_to_right = 'white' in state[house_to_right]['house_color']
                # If this house_color is green
                # BUT the house on the right is not white
                # THEN green must be removed from this house
                if has_green and not white_to_right:
                    state = einstein.remove_value(house, 'house_color', 'green', state)
                # If this house is NOT green
                # THEN the house to the right cannot be white
                elif not has_green and white_to_right:
                    state = einstein.remove_value(house_to_right, 'house_color', 'white', state)
        return state