Time to check the rest of the slopes — you need to minimize the probability of a sudden arboreal stop, after all.
Determine the number of trees you would encounter if, for each of the following slopes, you start at the top-left corner and traverse the map all the way to the bottom:
Along the same map we used before:
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
these slopes would find 2
, 7
, 3
, 4
, and 2
trees respectively.
Write a function count_trees
that takes three arguments: i) an integer \(r \in \mathbb{N}_0\) (int
), ii) an integer \(d \in \mathbb{N}_0\) (int
), and iii) the pathname (str
) of a text file containing a map of the open squares (.
) and trees (#
) on a slope. The function must return the number of trees (int
) you would encounter when starting at the top-left corner of the map and following a slope of right \(r\) and down \(d\).
In this interactive session we assume the text file slope.txt
1 to be located in the current directory.
>>> count_trees(1, 1, 'slope.txt')
2
>>> count_trees(3, 1, 'slope.txt')
7
>>> count_trees(5, 1, 'slope.txt')
3
>>> count_trees(7, 1, 'slope.txt')
4
>>> count_trees(1, 2, 'slope.txt')
2