01020304050607080910111213141516171819202122232425

Advent of Code

2017/4

High-Entropy Passphrases

in C#

by encse

A new system policy has been put in place that requires all accounts to use a passphrase instead of simply a password. A passphrase consists of a series of words (lowercase letters) separated by spaces.

To ensure security, a valid passphrase must contain no duplicate words.

Read the full puzzle.

using System;
using System.Linq;

namespace AdventOfCode.Y2017.Day04;

[ProblemName("High-Entropy Passphrases")]
class Solution : Solver {

    public object PartOne(string lines) =>
        ValidLineCount(lines, word => word);

    public object PartTwo(string lines) =>
        ValidLineCount(lines, word => string.Concat(word.OrderBy(ch => ch)));

    int ValidLineCount(string lines, Func<string, string> normalizer) =>
        lines.Split('\n').Where(line => IsValidLine(line.Split(' '), normalizer)).Count();

    bool IsValidLine(string[] words, Func<string, string> normalizer) =>
        words.Select(normalizer).Distinct().Count() == words.Count();
}

Please ☆ my repo if you like it!

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