39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
|
|
namespace AoC {
|
|
|
|
internal class Day6 {
|
|
|
|
internal void Solve() {
|
|
var readLines = File.ReadAllText("day6/input").Split("\n\n", StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
static ISet<char> UniqueCharactersExceptNewline(string s) => s.Where(c => c != '\n').ToImmutableHashSet();
|
|
int part1 = readLines.Select(UniqueCharactersExceptNewline).Sum(s => s.Count);
|
|
|
|
Console.WriteLine(part1);
|
|
|
|
static IEnumerable<IImmutableSet<char>> GroupToUniqueCharacters(string s) => s
|
|
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(s => s.ToImmutableHashSet());
|
|
|
|
static IImmutableSet<char> Intersection(IImmutableSet<char> first, IImmutableSet<char> second) =>
|
|
first.Intersect(second);
|
|
|
|
static IImmutableSet<char> IntersectAll(IEnumerable<IImmutableSet<char>> group) => group
|
|
.Skip(1)
|
|
.Aggregate(group.First(), Intersection);
|
|
|
|
int part2 = readLines
|
|
.Select(GroupToUniqueCharacters)
|
|
.Select(IntersectAll)
|
|
.Sum(s => s.Count);
|
|
|
|
Console.WriteLine(part2);
|
|
}
|
|
}
|
|
} |