Ejemplo n.º 1
0
    def test_slope_is_not_ended_when_starting_from_top(self):
        # given
        pattern = ['.#', '#.']

        # when
        map = Map(pattern=pattern)

        # then
        assert map.is_end_of_slope_reached(0) is False
Ejemplo n.º 2
0
    def test_slope_is_not_ended_when_reaching_another_level(self):
        # given
        pattern = ['.#', '#.', '#.']

        # when
        map = Map(pattern=pattern)

        # then
        assert map.is_end_of_slope_reached(1) is False
Ejemplo n.º 3
0
    def test_can_check_position_when_going_down_and_right(self):
        # given
        pattern = ['.#', '#.']

        # when
        map = Map(pattern=pattern)

        # then
        assert map.check_position(down=1, right=2) == '#'
Ejemplo n.º 4
0
    def test_slope_end_when_reaching_last_level(self):
        # given
        pattern = ['.#', '#.']

        # when
        map = Map(pattern=pattern)

        # then
        assert map.is_end_of_slope_reached(1) is True
Ejemplo n.º 5
0
    def test_can_draw_map_from_a_pattern(self):
        # given
        pattern = ['.#', '#.']

        # when
        map = Map(pattern=pattern)

        # then
        assert map.check_position(0, 0) == '.'
        assert map.check_position(0, 1) == '#'
        assert map.check_position(1, 0) == '#'
        assert map.check_position(1, 1) == '.'
Ejemplo n.º 6
0
    def test_can_check_position_when_going_down_and_right_with_a_bigger_pattern(
            self):
        # given
        pattern = [
            '.#.',
            '.#.',
        ]

        # when
        map = Map(pattern=pattern)

        # then
        assert map.check_position(down=1, right=3) == '.'
Ejemplo n.º 7
0
def count_trees_on_the_slope(map: Map, trajectory: Trajectory) -> int:
    position_down = 0
    position_right = 0
    counted_trees = 0

    while True:
        if map.is_end_of_slope_reached(position_down):
            break

        position_down = position_down + trajectory.down
        position_right = position_right + trajectory.right

        next_element = map.check_position(down=position_down,
                                          right=position_right)

        if next_element == '#':
            counted_trees = counted_trees + 1

    return counted_trees
Ejemplo n.º 8
0
    def test_should_count_no_tree_on_the_slope(self):
        # given
        trajectory = Trajectory(right=3, down=1)
        map = Map(pattern=[
            '..',
            '..'
        ])

        # when
        assert count_trees_on_the_slope(map, trajectory) == 0
Ejemplo n.º 9
0
    def test_should_count_1_tree_on_the_slope_with_example(self):
        # given
        trajectory = Trajectory(right=3, down=1)
        map = Map(pattern=[
            '..##.......',
            '#...#...#..',
            '.#....#..#.',
        ])

        # when
        assert count_trees_on_the_slope(map, trajectory) == 1
Ejemplo n.º 10
0
    def test_should_count_2_tree_on_different_trajectory(self, trajectory, tree_count):
        # given
        map = Map(pattern=[
            '..##.......',
            '#...#...#..',
            '.#....#..#.',
            '..#.#...#.#',
            '.#...##..#.',
            '..#.##.....',
            '.#.#.#....#',
            '.#........#',
            '#.##...#...',
            '#...##....#',
            '.#..#...#.#',
        ])

        # when
        assert count_trees_on_the_slope(map, trajectory) == tree_count
Ejemplo n.º 11
0
        position_down = position_down + trajectory.down
        position_right = position_right + trajectory.right

        next_element = map.check_position(down=position_down,
                                          right=position_right)

        if next_element == '#':
            counted_trees = counted_trees + 1

    return counted_trees


if __name__ == '__main__':
    file_reader = FileReader('./input')
    slope_plan = file_reader.to_str_list()
    map = Map(pattern=slope_plan)

    slope_1 = Trajectory(right=1, down=1)
    slope_2 = Trajectory(right=3, down=1)
    slope_3 = Trajectory(right=5, down=1)
    slope_4 = Trajectory(right=7, down=1)
    slope_5 = Trajectory(right=1, down=2)

    print('Exercise 1')
    print(count_trees_on_the_slope(map, slope_2))

    print('Exercise 2')
    print(count_trees_on_the_slope(map, slope_1))
    print(count_trees_on_the_slope(map, slope_2))
    print(count_trees_on_the_slope(map, slope_3))
    print(count_trees_on_the_slope(map, slope_4))