Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Day 6 Advent of Code - 2020
using System.Collections.Generic;
using System.Linq;
using AdventOfCode.Library;
namespace AdventOfCode
{
// Day 6
internal static class Program
{
private const bool DoPart1 = true;
private const bool DoPart2 = true;
public static void Main()
{
var data = TextUtility.ReadBlobs();
var customs = data.Select(cf => new CustomsForm(cf)).ToList();
data.Process(DoPart1, 1, customs, Solver1);
data.Process(DoPart2, 1, customs, Solver2);
}
private static string Solver1(List<string> arg1, object arg2)
{
var customs = (List<CustomsForm>) arg2;
int answer = customs.Select(c => c.DistinctQuestions).Sum();
return $"{answer}";
}
private static string Solver2(List<string> arg1, object arg2)
{
var customs = (List<CustomsForm>) arg2;
int answer = customs.Select(c => c.AllMatchedQuestions).Sum();
return $"{answer}";
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace AdventOfCode
{
[DebuggerDisplay("People: {People} - Distinct: {DistinctQuestions} - AllMatch: {AllMatchedQuestions}")]
public class CustomsForm
{
public readonly int People;
private readonly Dictionary<char, int> _tally;
public int DistinctQuestions => _tally.Keys.Count;
public int AllMatchedQuestions => _tally.Values.Count(c => c == People);
public CustomsForm(string s)
{
var answers = s.Split(" ", StringSplitOptions.RemoveEmptyEntries);
People = answers.Length;
_tally = new Dictionary<char, int>();
foreach (string answer in answers)
{
foreach (char c in answer)
{
if (!_tally.ContainsKey(c))
{
_tally[c] = 0;
}
_tally[c]++;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment