diff --git a/2017/puzzle-04-01.cc b/2017/puzzle-04-01.cc new file mode 100644 index 0000000..ab0af27 --- /dev/null +++ b/2017/puzzle-04-01.cc @@ -0,0 +1,37 @@ +#include +#include +#include + +auto split(std::string const& line, char c) -> std::vector +{ + std::vector 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 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; +} diff --git a/2017/puzzle-04-02.cc b/2017/puzzle-04-02.cc new file mode 100644 index 0000000..1015df2 --- /dev/null +++ b/2017/puzzle-04-02.cc @@ -0,0 +1,40 @@ +#include +#include +#include + +auto split(std::string const& line, char c) -> std::vector +{ + std::vector 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 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; +}