Add 2017 day 4 puzzles

This commit is contained in:
2021-12-13 14:51:53 +00:00
parent 9619bb1f35
commit 3e77d0aacc
2 changed files with 77 additions and 0 deletions

37
2017/puzzle-04-01.cc Normal file
View File

@@ -0,0 +1,37 @@
#include <iostream>
#include <string>
#include <vector>
auto split(std::string const& line, char c) -> std::vector<std::string>
{
std::vector<std::string> result;
std::size_t idx{0};
while (idx != std::string::npos) {
auto next{line.find(c, idx)};
if (next == std::string::npos) {
result.push_back(line.substr(idx));
idx = next;
}
else {
result.push_back(line.substr(idx, next - idx));
idx = next + 1;
}
}
return result;
}
auto main() -> int
{
std::string line;
unsigned valid{0};
while (std::getline(std::cin, line) && !line.empty()) {
std::vector<std::string> words{split(line, ' ')};
std::sort(words.begin(), words.end());
if (std::adjacent_find(words.begin(), words.end()) == words.end()) {
++valid;
}
}
std::cout << "Valid passphrases: " << valid << '\n';
return 0;
}

40
2017/puzzle-04-02.cc Normal file
View File

@@ -0,0 +1,40 @@
#include <iostream>
#include <string>
#include <vector>
auto split(std::string const& line, char c) -> std::vector<std::string>
{
std::vector<std::string> result;
std::size_t idx{0};
while (idx != std::string::npos) {
auto next{line.find(c, idx)};
if (next == std::string::npos) {
result.push_back(line.substr(idx));
idx = next;
}
else {
result.push_back(line.substr(idx, next - idx));
idx = next + 1;
}
}
return result;
}
auto main() -> int
{
std::string line;
unsigned valid{0};
while (std::getline(std::cin, line) && !line.empty()) {
std::vector<std::string> words{split(line, ' ')};
for (auto& w : words) {
std::sort(w.begin(), w.end());
}
std::sort(words.begin(), words.end());
if (std::adjacent_find(words.begin(), words.end()) == words.end()) {
++valid;
}
}
std::cout << "Valid passphrases: " << valid << '\n';
return 0;
}