diff options
Diffstat (limited to '2022/src')
| -rw-r--r-- | 2022/src/day06.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/2022/src/day06.cpp b/2022/src/day06.cpp new file mode 100644 index 0000000..44d5dc2 --- /dev/null +++ b/2022/src/day06.cpp | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <numeric> | ||
| 4 | #include <sstream> | ||
| 5 | #include <string> | ||
| 6 | #include <vector> | ||
| 7 | #include <set> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | string | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | return { istreambuf_iterator<char>{ input }, {} }; | ||
| 15 | } | ||
| 16 | |||
| 17 | void | ||
| 18 | solve(const string& data, size_t length) | ||
| 19 | { | ||
| 20 | for ( size_t i = 0; i != data.length() - length; ++i ) { | ||
| 21 | const auto substr = data.substr(i, length); | ||
| 22 | set<char> set(begin(substr), end(substr)); | ||
| 23 | if (set.size() == length) { | ||
| 24 | cout << i + length << endl; | ||
| 25 | break; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | int | ||
| 31 | main() | ||
| 32 | { | ||
| 33 | const auto data = read_file("data/day06.txt"); | ||
| 34 | solve(data, 4); | ||
| 35 | solve(data, 14); | ||
| 36 | } | ||
