diff options
Diffstat (limited to '2024/src/day25.cpp')
| -rw-r--r-- | 2024/src/day25.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/2024/src/day25.cpp b/2024/src/day25.cpp new file mode 100644 index 0000000..6d43ddf --- /dev/null +++ b/2024/src/day25.cpp | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <string> | ||
| 5 | #include <tuple> | ||
| 6 | #include <vector> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | using pattern_type = vector<string>; | ||
| 10 | |||
| 11 | vector<string> | ||
| 12 | split(string_view line, string_view delimiter) | ||
| 13 | { | ||
| 14 | size_t pos_start = 0; | ||
| 15 | size_t pos_end = 0; | ||
| 16 | |||
| 17 | vector<string> res; | ||
| 18 | |||
| 19 | while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { | ||
| 20 | auto token = line.substr(pos_start, pos_end - pos_start); | ||
| 21 | pos_start = pos_end + delimiter.length(); | ||
| 22 | |||
| 23 | res.emplace_back(token); | ||
| 24 | } | ||
| 25 | |||
| 26 | res.emplace_back(line.substr(pos_start)); | ||
| 27 | return res; | ||
| 28 | } | ||
| 29 | |||
| 30 | tuple<vector<pattern_type>, vector<pattern_type>> | ||
| 31 | read_file(string_view filename) | ||
| 32 | { | ||
| 33 | fstream input{ filename }; | ||
| 34 | const auto patterns{ split(string{ istreambuf_iterator<char>{ input }, {} }, "\n\n") }; | ||
| 35 | |||
| 36 | vector<pattern_type> keys; | ||
| 37 | vector<pattern_type> locks; | ||
| 38 | |||
| 39 | for ( const auto& pattern: patterns ) { | ||
| 40 | const pattern_type& lines = split(pattern, "\n"); | ||
| 41 | |||
| 42 | if ( lines[0] == "#####" && lines[6] == "....." ) { | ||
| 43 | locks.push_back(lines); | ||
| 44 | } | ||
| 45 | else if ( lines[0] == "....." && lines[6] == "#####" ) { | ||
| 46 | keys.push_back(lines); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | return { keys, locks }; | ||
| 51 | } | ||
| 52 | |||
| 53 | bool | ||
| 54 | fits(const pattern_type& key, const pattern_type& lock) | ||
| 55 | { | ||
| 56 | for ( size_t row = 0; row != key.size(); ++row ) { | ||
| 57 | for ( size_t col = 0; col != key[row].size(); ++col ) { | ||
| 58 | if ( key[row][col] == '#' && lock[row][col] == '#' ) { | ||
| 59 | return false; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | return true; | ||
| 64 | } | ||
| 65 | |||
| 66 | void | ||
| 67 | part1(const tuple<vector<pattern_type>, vector<pattern_type>>& data) | ||
| 68 | { | ||
| 69 | const auto [keys, locks] = data; | ||
| 70 | |||
| 71 | long count = 0; | ||
| 72 | for ( const auto& key: keys ) { | ||
| 73 | for ( const auto& lock: locks ) { | ||
| 74 | if ( fits(key, lock) ) { | ||
| 75 | ++count; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | cout << count << endl; | ||
| 80 | } | ||
| 81 | |||
| 82 | int | ||
| 83 | main() | ||
| 84 | { | ||
| 85 | const auto data = read_file("data/day25.txt"); | ||
| 86 | part1(data); | ||
| 87 | } | ||
