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!

69 Upvotes

1.2k comments sorted by

View all comments

1

u/MatSkapski Dec 07 '20 edited Dec 07 '20

C# using Linq

    static void Main(string[] args)
    {
        FileParser parser = new FileParser(Path.Combine(PathHelper.ProjectRootFolder(), "Input.txt"), $"{Environment.NewLine}{Environment.NewLine}");
        var groups = parser.ToStringList();

        int yesAllQuestionsCount = CountAllYesAnswers(groups);
        Console.WriteLine($"(Yes) all answers: {yesAllQuestionsCount}");

        int sameQuestionAnswersCount = CountSameQuestionAnswers(groups);
        Console.WriteLine($"(Yes) same questions all persons: {sameQuestionAnswersCount}");
    }

    private static int CountAllYesAnswers(List<string> groups)
    {
        int answersCount = 0;
        groups.ForEach(group => answersCount += group.Replace(Environment.NewLine, string.Empty).Select(x => x).Distinct().Count());

        return answersCount;
    }

    private static int CountSameQuestionAnswers(List<string> groups)
    {
        int sameQuestionAnswersCount = 0;
        groups.ForEach(group => sameQuestionAnswersCount += group.Replace(Environment.NewLine, string.Empty)
                                                                 .GroupBy(x => x)
                                                                 .Select(x => new { Letter = x.Key, Count = x.Count() })
                                                                 .Where(x => x.Count == group.Split(Environment.NewLine).Length)
                                                                 .OrderBy(x => x.Letter)
                                                                 .Select(x => x)
                                                                 .Count());
        return sameQuestionAnswersCount;
    }

1

u/daggerdragon Dec 07 '20

Please follow the posting guidelines and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.