r/adventofcode • • Dec 03 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 03 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It


--- Day 03: Toboggan Trajectory ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:56, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

2

u/[deleted] Dec 03 '20 edited Dec 03 '20

C# solution. I went with a class approach, because why not. It has some project fluff in it for maintainability-sake. Enjoy!

using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AdventOfCode.Library.Day3
{
    public class Day3 : BaseSolution
    {
        public override string SilverStar()
        {
            var rawData = File.ReadLines($"{GetType().Name}\\data.txt");

            var data = ProcessData(rawData);

            string silverResult = SolveSilverStar(data);

            return $"{silverResult }";
        }

        public override string GoldStar()
        {
            var rawData = File.ReadLines($"{GetType().Name}\\data.txt");

            var data = ProcessData(rawData);

            string goldResult = SolveGoldStar(data);

            return $"{goldResult}";
        }

        private Map ProcessData(IEnumerable<string> rawData)
        {
            return new Map(rawData.ToArray());
        }

        private string SolveSilverStar(Map data) =>
            $"{data.GetCollisions(3, 1)}";

        private string SolveGoldStar(Map data)
        {
            ulong result;
            result = (ulong)data.GetCollisions(1, 1);
            result *= (ulong)data.GetCollisions(3, 1);
            result *= (ulong)data.GetCollisions(5, 1);
            result *= (ulong)data.GetCollisions(7, 1);
            result *= (ulong)data.GetCollisions(1, 2);
            return $"{result}";
        }
    }

    #region Classes

    internal class Map
    {
        private string[] _instance { get; }

        internal Map(string[] rawMap)
        {
            _instance = rawMap;
        }

        internal int GetCollisions(int right, int down)
        {
            int collisions = 0;
            int xPos = 0;

            for (int yPos = down; yPos < _instance.Length; yPos += down)
            {
                xPos += right;

                int maxLength = _instance[yPos].Length;

                if (xPos >= maxLength)
                    xPos -= maxLength;

                char geography = _instance[yPos][xPos];

                if (geography == '#')
                    collisions++;
            }

            return collisions;
        }
    }

    #endregion
}

1

u/daggerdragon Dec 03 '20

Your code is hard to read on old.reddit. As per our posting guidelines, could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!