01020304050607080910111213141516171819202122232425

Advent of Code

2020/6

Custom Customs

in C#

by encse

As your flight approaches the regional airport where you'll switch to a much larger plane, customs declaration forms are distributed to the passengers.

The form asks a series of 26 yes-or-no questions marked a through z. All you need to do is identify the questions for which anyone in your group answers "yes". Since your group is just you, this doesn't take very long.

Read the full puzzle.

using System;
using System.Collections.Immutable;
using System.Linq;

namespace AdventOfCode.Y2020.Day06;

[ProblemName("Custom Customs")]      
class Solution : Solver {

    public object PartOne(string input) => Solve(input, (a,b) => a.Union(b));
    public object 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();
    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator