Write a function named toboggan
that simulates a toboggan sliding down a repeating ski slope and returns the number of trees it would hit.
A map of the ski slope is stored in a file whose name is passed to your function as a parameter.
The file contains a grid of open squares (.
) and trees (#
) such as the following file, slope.txt
:
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
You should simulate the toboggan's movement down this slope.
The toboggan starts on the square in the top-left corner and needs to reach the bottom (below the bottom-most row on your map).
The toboggan repeatedly jumps right 3, down 1 until it reaches the bottom.
If the toboggan hits the right edge of the slope, it wraps around to the left edge.
So for the example input file above, the toboggan's path would be the following, where O
represents a square the toboggan touched without hitting a tree and X
represents a square the toboggan touched and did hit a tree:
O.##.......
#..O#...#..
.#....X..#.
..#.#...#O#
.X...##..#.
..#.X#.....
.#.#.#.O..#
.#........X
#.X#...#...
#...#X....#
.#..#...X.#
Therefore the call of toboggan("slope.txt")
would return 7
since it hit 7 trees on its way to the bottom of the slope.
You may assume that the file exists and is readable, that it follows the format described above,
and that the slope is of size at least 2x2.