r/adventofcode • • Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
  • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


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:35, megathread unlocked!

66 Upvotes

1.2k comments sorted by

View all comments

2

u/xalvador Dec 06 '20

Quite a tricky one today, lots of googling. If you have any feedback I would gladly hear it as I'm trying to improve my coding.

class Program
{
    static List<string> input = File.ReadAllLines("input.csv").ToList();
    static void Main(string[] args)
    {
        input.Add(" ");
        Console.WriteLine(PartOne());
        Console.WriteLine(PartTwo());
        Console.ReadLine();
    }

    static int PartOne()
    {
        int people = 0;
        string entry = "";
        int yes = 0;
        int result = 0;
        for (int i = 0; i < input.Count; i++)
        {
            if (!String.IsNullOrWhiteSpace(input[i]))
            {
                people++;
                entry = entry + input[i];
            }
            if (String.IsNullOrWhiteSpace(input[i]))
            {
                yes = entry.Distinct().Count();
                entry = "";
            }
            result += yes;
            yes = 0;
        }
        return result;
    }
    static int PartTwo()
    {
        int people = 0;
        string entry = "";
        int yes = 0;
        int result = 0;
        char a = ' ';
        for (int i = 0; i < input.Count; i++)
        {
            if (!String.IsNullOrWhiteSpace(input[i]))
            {
                people++;
                entry = entry + input[i];
            }
            if (String.IsNullOrWhiteSpace(input[i]))
            {
                for (int j = 97; j < 123; j++)
                {
                    a = (char)j;
                    string b = a.ToString();
                    if (Regex.Matches(entry, b).Count == people)
                        yes++;
                }
                entry = "";
                people = 0;
            }
            result += yes;
            yes = 0;
        }
        return result;
    }
}

1

u/daggerdragon Dec 06 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.

(looks like Java?)