diff options
Diffstat (limited to '2017/src/day04.cpp')
| -rw-r--r-- | 2017/src/day04.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/2017/src/day04.cpp b/2017/src/day04.cpp new file mode 100644 index 0000000..01e89b2 --- /dev/null +++ b/2017/src/day04.cpp | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <filesystem> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <set> | ||
| 6 | #include <sstream> | ||
| 7 | #include <string> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | vector<string> | ||
| 14 | read_lines(const filesystem::path& filename) | ||
| 15 | { | ||
| 16 | ifstream file{ filename }; | ||
| 17 | vector<string> lines; | ||
| 18 | |||
| 19 | for ( string line; getline(file, line); ) { | ||
| 20 | lines.emplace_back(line); | ||
| 21 | } | ||
| 22 | |||
| 23 | return lines; | ||
| 24 | } | ||
| 25 | |||
| 26 | vector<string> | ||
| 27 | split(const string& line) | ||
| 28 | { | ||
| 29 | stringstream strm{ line }; | ||
| 30 | vector<string> words; | ||
| 31 | |||
| 32 | for ( string word; strm >> word; ) { | ||
| 33 | words.emplace_back(word); | ||
| 34 | } | ||
| 35 | |||
| 36 | return words; | ||
| 37 | } | ||
| 38 | |||
| 39 | void | ||
| 40 | part1(const vector<string>& lines) | ||
| 41 | { | ||
| 42 | auto num = ranges::count_if(lines, [](const string& line) { | ||
| 43 | auto words = split(line); | ||
| 44 | set<string> set{ words.begin(), words.end() }; | ||
| 45 | |||
| 46 | return set.size() == words.size(); | ||
| 47 | }); | ||
| 48 | |||
| 49 | cout << "Part1: " << num << '\n'; | ||
| 50 | } | ||
| 51 | |||
| 52 | void | ||
| 53 | part2(const vector<string>& lines) | ||
| 54 | { | ||
| 55 | auto num = ranges::count_if(lines, [](const string& line) { | ||
| 56 | auto words = split(line); | ||
| 57 | set<string> set; | ||
| 58 | |||
| 59 | for ( auto word: words ) { | ||
| 60 | ranges::sort(word); | ||
| 61 | set.insert(word); | ||
| 62 | } | ||
| 63 | |||
| 64 | return set.size() == words.size(); | ||
| 65 | }); | ||
| 66 | |||
| 67 | cout << "Part2: " << num << '\n'; | ||
| 68 | } | ||
| 69 | |||
| 70 | } // namespace | ||
| 71 | |||
| 72 | int | ||
| 73 | main() | ||
| 74 | { | ||
| 75 | auto lines = read_lines("data/day04.txt"); | ||
| 76 | part1(lines); | ||
| 77 | part2(lines); | ||
| 78 | } | ||
