DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 3: Toboggan Trajectory

Collapse
 
dirkfraanje profile image
Dirk Fraanje (the Netherlands) • Edited

My solution in C#:

public static class Day3
{
    static int r;
    static int d;
    static string[] input = new string[] { //input };
    static Stopwatch timer = new Stopwatch();

    static int trees = 0;

    public static void Execute()
    {
        timer.Start();
        findNextLocationAndUpdateTreeCount();
    }

    private static void findNextLocationAndUpdateTreeCount()
    {
        var line = input[d];
        var positionOnLine = line.ToCharArray()[r];
        if (positionOnLine == '#')
            trees++;
        //For part 2 change r and d to the values you have to check
        r += 3;
        d += 1;
        if (input.Length <= d) {
            timer.Stop();
            Console.WriteLine($"Answer: {trees} trees");
            Console.WriteLine($"Executed in: {timer.ElapsedMilliseconds} milliseconds ({timer.ElapsedTicks}  Ticks)");
            Console.ReadLine();
        }
        if(line.Length <= r)
        {
            //By using r - line.Length you don't have to copy the whole input when you reached the end on the right (also possible to use Mod)
            r = r - line.Length;
        }
        findNextLocationAndUpdateTreeCount();
    }


}
Enter fullscreen mode Exit fullscreen mode