mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-12-22 16:35:18 +00:00
Added day 2 part 2
This commit is contained in:
parent
a29b28b303
commit
beb7b447e9
|
@ -1,11 +1,20 @@
|
|||
import std.algorithm;
|
||||
import std.format;
|
||||
import std.functional;
|
||||
import std.stdio;
|
||||
|
||||
|
||||
import dayutil;
|
||||
|
||||
bool isPasswordValid(T)(T line) {
|
||||
void run(string[] args) {
|
||||
ulong count = parts!ulong(args,
|
||||
() => stdin.byLine.count!(l => isPasswordValid1(l)),
|
||||
() => stdin.byLine.count!(l => isPasswordValid2(l)));
|
||||
writeln(count);
|
||||
}
|
||||
|
||||
/// Part 1
|
||||
bool isPasswordValid1(T)(T line) {
|
||||
int min, max;
|
||||
char c;
|
||||
string password;
|
||||
|
@ -14,14 +23,28 @@ bool isPasswordValid(T)(T line) {
|
|||
return min <= charCount && charCount <= max;
|
||||
}
|
||||
|
||||
void run(string[] args) {
|
||||
ulong count = stdin.byLine
|
||||
.count!(l => isPasswordValid(l));
|
||||
writeln(count);
|
||||
unittest {
|
||||
assert(isPasswordValid1("1-3 a: abcde") == true);
|
||||
assert(isPasswordValid1("1-3 b: cdefg") == false);
|
||||
assert(isPasswordValid1("2-9 c: ccccccccc") == true);
|
||||
}
|
||||
|
||||
/// Part 2
|
||||
bool isPasswordValid2(T)(T line) {
|
||||
int min, max;
|
||||
char c;
|
||||
string password;
|
||||
line.formattedRead("%d-%d %c: %s", min, max, c, password);
|
||||
// 1 2 | R
|
||||
// T T | F
|
||||
// T F | T
|
||||
// F T | T
|
||||
// F F | F
|
||||
return (password[min - 1] == c) != (password[max - 1] == c);
|
||||
}
|
||||
|
||||
unittest {
|
||||
assert(isPasswordValid("1-3 a: abcde") == true);
|
||||
assert(isPasswordValid("1-3 b: cdefg") == false);
|
||||
assert(isPasswordValid("2-9 c: ccccccccc") == true);
|
||||
assert(isPasswordValid2("1-3 a: abcde") == true);
|
||||
assert(isPasswordValid2("1-3 b: cdefg") == false);
|
||||
assert(isPasswordValid2("2-9 c: ccccccccc") == false);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue