aoc2020/day2/part1/main.d

31 lines
823 B
D

import std;
void main()
{
auto passwords = slurp!(ulong, ulong, char, string)("input", "%s-%s %s: %s");
passwords.findNumberOfValidPasswords.writeln;
}
alias PasswordWithPolicy = Tuple!(ulong, ulong, char, string);
ulong findNumberOfValidPasswords(PasswordWithPolicy[] passwords)
{
return passwords.filter!(password => password.isPasswordValid).count;
}
bool isPasswordValid(PasswordWithPolicy password)
{
immutable numberOfRequiredLetter = password[3].byChar.filter!(c => c == password[2]).count;
return numberOfRequiredLetter >= password[0] && numberOfRequiredLetter <= password[1];
}
unittest
{
auto input = [
tuple(1UL, 3UL, 'a', "abcde"),
tuple(1UL, 3UL, 'b', "cdefg"),
tuple(2UL, 9UL, 'c', "ccccccccc")
];
assert(input.findNumberOfValidPasswords == 2);
}