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 countTrees
that takes three arguments: i) an integer \(r \in \mathbb{N}_0\) (Number
), ii) an integer \(d \in \mathbb{N}_0\) (Number
), and iii) the pathname (String
) of a text file containing a map of the open squares (.
) and trees (#
) on a slope. The function must return the number of trees (Number
) 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.
> countTrees(1, 1, "slope.txt")
2
> countTrees(3, 1, "slope.txt")
7
> countTrees(5, 1, "slope.txt")
3
> countTrees(7, 1, "slope.txt")
4
> countTrees(1, 2, "slope.txt")
2