day: solve part 1 & 2
This commit is contained in:
parent
177b08e515
commit
2b24063138
4 changed files with 2067 additions and 0 deletions
1000
day2/part1/input
Normal file
1000
day2/part1/input
Normal file
File diff suppressed because it is too large
Load diff
31
day2/part1/main.d
Normal file
31
day2/part1/main.d
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
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);
|
||||||
|
}
|
1000
day2/part2/input
Normal file
1000
day2/part2/input
Normal file
File diff suppressed because it is too large
Load diff
36
day2/part2/main.d
Normal file
36
day2/part2/main.d
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Reference in a new issue