Exemple #1
0
def part_2(inputs: tuple[str]) -> int:
    assert len(inputs) == 1
    santa_navigation = NavigationWithHeading(Position(0, 0), NORTH)
    robot_navigation = NavigationWithHeading(Position(0, 0), NORTH)
    for i, c in enumerate(inputs[0]):
        if i % 2 != 0:
            _add_navigation_instruction(robot_navigation, c)
        else:
            _add_navigation_instruction(santa_navigation, c)
    visited = santa_navigation.get_visited_positions(True)
    visited.extend(robot_navigation.get_visited_positions(True))
    return _count_unique_positions(visited)
    def test_navigation_with_heading(self):
        navigation = NavigationWithHeading(
            Position(0, 0), Headings["N"].value,
            lambda pos: pos.x <= 4 and pos.y <= 4)
        navigation.drift(Headings["N"].value, 1)
        navigation.left(90)
        navigation.forward(1)
        navigation.right(180)
        navigation.forward(10)

        self.assertEqual(navigation.position, Position(4, 1))
        visited_positions = navigation.get_visited_positions(True)
        self.assertEqual(
            visited_positions,
            [Position(0, 0),
             Position(0, 1),
             Position(-1, 1),
             Position(4, 1)])
        visited_positions = navigation.get_visited_positions(False)
        self.assertEqual(
            visited_positions,
            [Position(0, 1), Position(-1, 1),
             Position(4, 1)])
Exemple #3
0
def part_1(inputs: tuple[str]) -> int:
    assert len(inputs) == 1
    navigation = NavigationWithHeading(Position(0, 0), NORTH)
    for c in inputs[0]:
        _add_navigation_instruction(navigation, c)
    return _count_unique_positions(navigation.get_visited_positions(True))