Refactor day 2 to use lambdas
parent
18b9e2b123
commit
fcdb8533bf
|
@ -94,31 +94,40 @@ class Entry {
|
||||||
std::string password;
|
std::string password;
|
||||||
};
|
};
|
||||||
|
|
||||||
int part1(const std::vector<std::string> &input) {
|
/**
|
||||||
|
* Get the number of valid passwords in the input
|
||||||
|
* @param input The input split into lines
|
||||||
|
* @param matches A function that checks whether or not the password matches the policy
|
||||||
|
* @return int The number ofm atching password
|
||||||
|
*/
|
||||||
|
int getNumValidPasswords(const std::vector<std::string> &input, const std::function<bool(const Entry &)> &valid) {
|
||||||
int numCorrect = 0;
|
int numCorrect = 0;
|
||||||
for (std::string rawEntry : input) {
|
for (std::string rawEntry : input) {
|
||||||
Entry entry = Entry::parse(rawEntry);
|
Entry entry = Entry::parse(rawEntry);
|
||||||
const Policy &policy = entry.getPolicy();
|
numCorrect += valid(entry);
|
||||||
const std::string &password = entry.getPassword();
|
|
||||||
char letter = entry.getPolicy().getLetter();
|
|
||||||
int count = std::count_if(password.begin(), password.end(), [=](char c) { return c == letter; });
|
|
||||||
numCorrect += (count >= policy.getMin() && count <= policy.getMax());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return numCorrect;
|
return numCorrect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int part1(const std::vector<std::string> &input) {
|
||||||
|
return getNumValidPasswords(input, [](const Entry &entry) {
|
||||||
|
const Policy &policy = entry.getPolicy();
|
||||||
|
const std::string &password = entry.getPassword();
|
||||||
|
int count = std::count_if(password.begin(), password.end(), [&](char c) { return c == policy.getLetter(); });
|
||||||
|
|
||||||
|
return (count >= policy.getMin() && count <= policy.getMax());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
int part2(const std::vector<std::string> &input) {
|
int part2(const std::vector<std::string> &input) {
|
||||||
int numCorrect = 0;
|
return getNumValidPasswords(input, [](const Entry &entry) {
|
||||||
for (std::string rawEntry : input) {
|
|
||||||
Entry entry = Entry::parse(rawEntry);
|
|
||||||
const Policy &policy = entry.getPolicy();
|
const Policy &policy = entry.getPolicy();
|
||||||
const std::string &password = entry.getPassword();
|
const std::string &password = entry.getPassword();
|
||||||
char letter = entry.getPolicy().getLetter();
|
char letter = entry.getPolicy().getLetter();
|
||||||
numCorrect += ((password.at(policy.getMin() - 1) == letter) ^ (password.at(policy.getMax() - 1) == letter));
|
|
||||||
}
|
|
||||||
|
|
||||||
return numCorrect;
|
return ((password.at(policy.getMin() - 1) == letter) ^ (password.at(policy.getMax() - 1) == letter));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
Loading…
Reference in New Issue