01020304050607080910111213141516171819202122232425

Advent of Code

2020/3

Toboggan Trajectory

in C#

by encse

With the toboggan login problems resolved, you set off toward the airport. While travel by toboggan might be easy, it's certainly not safe: there's very minimal steering and the area is covered in trees. You'll need to see which angles will take you near the fewest trees.

Due to the local geology, trees in this area only grow on exact integer coordinates in a grid. You make a map (your puzzle input) of the open squares (.) and trees (#) you can see. For example:

Read the full puzzle.

namespace AdventOfCode.Y2020.Day03;

[ProblemName("Toboggan Trajectory")]
class Solution : Solver {

    public object PartOne(string input) => TreeCount(input, (1, 3));
    public object PartTwo(string input) => TreeCount(input, (1, 1), (1, 3), (1, 5), (1, 7), (2, 1));

    long TreeCount(string input, params (int drow, int dcol)[] slopes) {
        var lines = input.Split("\n");
        var (crow, ccol) = (lines.Length, lines[0].Length);
        var mul = 1L;

        foreach (var (drow, dcol) in slopes) {
            var (irow, icol) = (drow, dcol);
            var trees = 0;
            while (irow < crow) {
                if (lines[irow][icol % ccol] == '#') {
                    trees++;
                }
                (irow, icol) = (irow + drow, icol + dcol);
            }
            mul *= trees;
        }
        return mul;
    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator