r/backtickbot • • Dec 06 '20

https://np.reddit.com/r/adventofcode/comments/k7ndux/2020_day_06_solutions/gesdcsi/

C#, turned out quite readable imho

int PartOne(string input) => Solve(input, (a,b) => a.Union(b));
int PartTwo(string input) => Solve(input, (a,b) => a.Intersect(b));

int Solve(string input, Func<ImmutableHashSet<char>, ImmutableHashSet<char>, ImmutableHashSet<char>> combine) {
    return (
        from grp in input.Split("\n\n")
        let answers = from line in grp.Split("\n") select line.ToImmutableHashSet()
        select answers.Aggregate(combine).Count
    ).Sum();
}

https://github.com/encse/adventofcode/blob/master/2020/Day06/Solution.cs

1 Upvotes

0 comments sorted by