Day 6
This commit is contained in:
2190
06input.txt
Normal file
2190
06input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -22,6 +22,9 @@
|
||||
<None Update="05input.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="06input.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -9,6 +9,7 @@
|
||||
Q03.Go();
|
||||
Q04.Go();
|
||||
Q05.Go();
|
||||
Q06.Go();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
75
Q06.cs
Normal file
75
Q06.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace _2020
|
||||
{
|
||||
class Q06
|
||||
{
|
||||
class group
|
||||
{
|
||||
public int numInGroup;
|
||||
public Dictionary<char, int> answers;
|
||||
}
|
||||
static List<group> list = new List<group>();
|
||||
|
||||
public static void Go()
|
||||
{
|
||||
MakeList();
|
||||
Part1();
|
||||
Part2();
|
||||
}
|
||||
|
||||
static void MakeList()
|
||||
{
|
||||
using var fs = new FileStream("06input.txt", FileMode.Open);
|
||||
using var sr = new StreamReader(fs);
|
||||
string line;
|
||||
group entry = null;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (line.Length == 0 || entry == null)
|
||||
{
|
||||
entry = new group()
|
||||
{
|
||||
numInGroup = 0,
|
||||
answers = new Dictionary<char, int>(),
|
||||
};
|
||||
list.Add(entry);
|
||||
}
|
||||
|
||||
if (line.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
entry.numInGroup++;
|
||||
foreach (var ch in line)
|
||||
{
|
||||
if (!entry.answers.ContainsKey(ch))
|
||||
{
|
||||
entry.answers.Add(ch, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.answers[ch]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Part1()
|
||||
{
|
||||
var sum = list.Sum(x => x.answers.Keys.Count);
|
||||
Debug.WriteLine($"Q06Part1: sum={sum}");
|
||||
}
|
||||
|
||||
static void Part2()
|
||||
{
|
||||
var sum = list.Sum(x => x.answers.Sum(y => y.Value == x.numInGroup ? 1 : 0));
|
||||
Debug.WriteLine($"Q06Part2: sum={sum}");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user