aoc2020/day2/part1/main.d

32 lines
800 B
D
Raw Normal View History

2020-12-02 10:46:41 +01:00
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)
{
2020-12-02 11:59:13 +01:00
return passwords.filter!isPasswordValid.count;
2020-12-02 10:46:41 +01:00
}
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);
}