Add 2017 day 4 puzzles
This commit is contained in:
37
2017/puzzle-04-01.cc
Normal file
37
2017/puzzle-04-01.cc
Normal 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
40
2017/puzzle-04-02.cc
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user