Compare commits
2 Commits
aedc195562
...
7438fc97a9
Author | SHA1 | Date | |
---|---|---|---|
7438fc97a9 | |||
b632755e96 |
92
2020/Day06.cs
Normal file
92
2020/Day06.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using static System.Console;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Xunit;
|
||||
|
||||
namespace AdventOfCode {
|
||||
|
||||
public class Day06 {
|
||||
|
||||
static int CountAnswers1(string[] input) {
|
||||
int total = 0;
|
||||
var groupAnswers = new HashSet<char>();
|
||||
foreach (string line in input) {
|
||||
if (line == "") {
|
||||
total += groupAnswers.Count();
|
||||
groupAnswers.Clear();
|
||||
continue;
|
||||
}
|
||||
foreach (char c in line) {
|
||||
groupAnswers.Add(c);
|
||||
}
|
||||
}
|
||||
total += groupAnswers.Count();
|
||||
return total;
|
||||
}
|
||||
|
||||
static int CountAnswers2(string[] input) {
|
||||
int total = 0;
|
||||
var groupAnswers = new HashSet<char>();
|
||||
int groupSize = 0;
|
||||
foreach (string line in input) {
|
||||
if (line == "") {
|
||||
total += groupAnswers.Count();
|
||||
groupAnswers.Clear();
|
||||
groupSize = 0;
|
||||
continue;
|
||||
}
|
||||
groupSize++;
|
||||
if (groupSize == 1) {
|
||||
foreach (char c in line) {
|
||||
groupAnswers.Add(c);
|
||||
}
|
||||
} else {
|
||||
foreach (char c in groupAnswers) {
|
||||
if (!line.Contains(c)) {
|
||||
groupAnswers.Remove(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
total += groupAnswers.Count();
|
||||
return total;
|
||||
}
|
||||
|
||||
static int Part1() {
|
||||
string[] input = File.ReadAllLines(Util.RootDir + "day06.txt");
|
||||
return CountAnswers1(input);
|
||||
}
|
||||
|
||||
static int Part2() {
|
||||
string[] input = File.ReadAllLines(Util.RootDir + "day06.txt");
|
||||
return CountAnswers2(input);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void Test() {
|
||||
string[] example =
|
||||
@"abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b".Split('\n');
|
||||
Assert.Equal(11, CountAnswers1(example));
|
||||
Assert.Equal(6, CountAnswers2(example));
|
||||
|
||||
Assert.Equal(6947, Part1());
|
||||
Assert.Equal(3398, Part2());
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ namespace AdventOfCode {
|
||||
|
||||
public class Program {
|
||||
static void Main(string[] args) {
|
||||
Day05.Test();
|
||||
Day06.Test();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2239
2020/day06.txt
Normal file
2239
2020/day06.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user