aboutsummaryrefslogtreecommitdiff
path: root/challenge-285/jtimothyking/csharp/ch-1.cs
blob: d767a65cb7c4ecf6628f7c632fb65142e0d710c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace _1_no_connection;

public static class NoConnection
{
    public static async Task Main()
    {
        // Read the input, a series of lines in the format "start end", followed by a blank line.
        var connections = await Console.In.ReadLinesAsync()
            .TakeWhile(line => !string.IsNullOrWhiteSpace(line))
            .Select(line => line.Trim().Split(' ', 2, StringSplitOptions.RemoveEmptyEntries))
            .Select(parts => (start: parts[0], end: parts[1]))
            .ToListAsync();

        var startPoints = connections.Select(connection => connection.start).ToHashSet();
        var unconnectedEndpoints = connections.Select(connection => connection.end)
            .Where(endpoint => !startPoints.Contains(endpoint))
            .ToHashSet();
        foreach (var endpoint in unconnectedEndpoints.Order())
            Console.WriteLine(endpoint);
    }

    private static async IAsyncEnumerable<string> ReadLinesAsync(this TextReader reader)
    {
        while (await reader.ReadLineAsync() is { } line)
            yield return line;
    }
}