37 lines
929 B
D
37 lines
929 B
D
|
module day2.part2.main;
|
||
|
|
||
|
import std;
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
const passwords = slurp!(size_t, size_t, char, string)("input", "%s-%s %s: %s");
|
||
|
passwords.findNumberOfValidPasswords.writeln;
|
||
|
}
|
||
|
|
||
|
alias PasswordWithPolicy = Tuple!(size_t, size_t, char, string);
|
||
|
|
||
|
ulong findNumberOfValidPasswords(const PasswordWithPolicy[] passwords)
|
||
|
{
|
||
|
return passwords.filter!(password => password.isPasswordValid).count;
|
||
|
}
|
||
|
|
||
|
bool isPasswordValid(const PasswordWithPolicy password)
|
||
|
{
|
||
|
immutable pos1 = password[0] - 1;
|
||
|
immutable pos2 = password[1] - 1;
|
||
|
immutable character = password[2];
|
||
|
immutable actualPassword = password[3];
|
||
|
return (actualPassword[pos1] == character) != (actualPassword[pos2] == character);
|
||
|
}
|
||
|
|
||
|
unittest
|
||
|
{
|
||
|
immutable input = [
|
||
|
tuple(1UL, 3UL, 'a', "abcde"),
|
||
|
tuple(1UL, 3UL, 'b', "cdefg"),
|
||
|
tuple(2UL, 9UL, 'c', "ccccccccc")
|
||
|
];
|
||
|
|
||
|
assert(input.findNumberOfValidPasswords == 1);
|
||
|
}
|