Ejemplo n.º 1
0
class TestTree(TestCase):
    def setUp(self):
        self.root = Step(None, (0, 0), "O")
        self.leaf1 = self.root.add_leaf(1, ".")
        self.leaf11 = self.leaf1.add_leaf(1, ".")
        self.leaf113 = self.leaf11.add_leaf(3, ".")
        self.leaf1132 = self.leaf113.add_leaf(2, ".")

    def test_terminal(self):
        self.assertSequenceEqual([self.leaf1132],
                                 self.root.get_terminal_leafs())

    def test_loop(self):
        self.leaf11324 = self.leaf1132.add_leaf(4, 'X')
        self.assertIsNone(self.leaf11324)

    def test_traverse(self):
        for node in self.leaf1132.go_upstream():
            print(node.coords, node.depth, node.value)

    def test_search(self):
        self.assertIsNone(self.root.search_by_value('X'))
        self.assertSequenceEqual(self.root.search_by_value("O"), [self.root])
        self.assertSetEqual(
            set(self.root.search_by_value(".")),
            {self.leaf1, self.leaf11, self.leaf113, self.leaf1132})

    def test_print(self):
        self.assertEqual(str(self.leaf11), "(0, -2) - .")
Ejemplo n.º 2
0
def do_step(upstream, direction):
    while True:
        try:
            computer.run()
        except Processor.ProcessorInputException:
            computer.push_input(direction)
        except Processor.ProcessorOutputException:
            result = computer.read_output()
            next_step = upstream.add_leaf(direction, result)
            # We are at the oxygen station, no need to go further
            if result == 2:
                withdraw(direction)
            # We have clear way and no loop on current path
            if result == 1 and next_step:
                for i in range(1, 5):
                    # Do not try to go back, push forward :)
                    if direction_map[i] == direction:
                        continue
                    do_step(next_step, i)
                withdraw(direction)
            return


do_step(step_tree, 1)
do_step(step_tree, 2)
do_step(step_tree, 3)
do_step(step_tree, 4)

# Subtract -1 for root node, it does not count as a step
print(min(x.depth for x in step_tree.search_by_value(2)) - 1)