diff options
| -rw-r--r-- | 2024/src/day12.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/2024/src/day12.cpp b/2024/src/day12.cpp new file mode 100644 index 0000000..6a23730 --- /dev/null +++ b/2024/src/day12.cpp | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <queue> | ||
| 5 | #include <set> | ||
| 6 | #include <string> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | using pos_type = tuple<size_t, size_t>; | ||
| 12 | |||
| 13 | map<pos_type, char> | ||
| 14 | read_file(string_view filename) | ||
| 15 | { | ||
| 16 | fstream input{ filename }; | ||
| 17 | map<pos_type, char> data; | ||
| 18 | |||
| 19 | size_t yPos = 0; | ||
| 20 | for ( string line; getline(input, line); ) { | ||
| 21 | for ( size_t xPos = 0; xPos != line.size(); ++xPos ) { | ||
| 22 | data[{ xPos, yPos }] = line.at(xPos); | ||
| 23 | } | ||
| 24 | ++yPos; | ||
| 25 | } | ||
| 26 | return data; | ||
| 27 | } | ||
| 28 | |||
| 29 | constexpr vector<pos_type> | ||
| 30 | neighbors(pos_type pos) | ||
| 31 | { | ||
| 32 | const auto [x, y] = pos; | ||
| 33 | return { { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } }; | ||
| 34 | } | ||
| 35 | |||
| 36 | void | ||
| 37 | part1(const map<pos_type, char>& data) | ||
| 38 | { | ||
| 39 | auto data_copy = data; | ||
| 40 | |||
| 41 | auto flood_fill = [&](pos_type start) { | ||
| 42 | set<pos_type> area; | ||
| 43 | queue<pos_type> queue; | ||
| 44 | queue.push(start); | ||
| 45 | |||
| 46 | while ( !queue.empty() ) { | ||
| 47 | auto pos = queue.front(); | ||
| 48 | queue.pop(); | ||
| 49 | |||
| 50 | if ( !data_copy.contains(pos) ) { | ||
| 51 | continue; | ||
| 52 | } | ||
| 53 | |||
| 54 | auto digit = data_copy.at(pos); | ||
| 55 | |||
| 56 | for ( const auto& neighbor: neighbors(pos) ) { | ||
| 57 | if ( !data_copy.contains(neighbor) ) { | ||
| 58 | continue; | ||
| 59 | } | ||
| 60 | if ( data_copy.at(neighbor) != digit ) { | ||
| 61 | continue; | ||
| 62 | } | ||
| 63 | queue.push(neighbor); | ||
| 64 | } | ||
| 65 | |||
| 66 | area.insert(pos); | ||
| 67 | data_copy.erase(pos); | ||
| 68 | } | ||
| 69 | return area; | ||
| 70 | }; | ||
| 71 | |||
| 72 | auto count_edges = [](const set<pos_type>& area) -> size_t { | ||
| 73 | size_t sum = 0; | ||
| 74 | |||
| 75 | for ( const auto& pos: area ) { | ||
| 76 | for ( const auto& neighbor: neighbors(pos) ) { | ||
| 77 | if ( !area.contains(neighbor) ) { | ||
| 78 | ++sum; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | return sum; | ||
| 84 | }; | ||
| 85 | |||
| 86 | size_t sum = 0; | ||
| 87 | while ( !data_copy.empty() ) { | ||
| 88 | auto area = flood_fill(data_copy.begin()->first); | ||
| 89 | auto edges = count_edges(area); | ||
| 90 | |||
| 91 | sum += area.size() * edges; | ||
| 92 | } | ||
| 93 | cout << sum << endl; | ||
| 94 | } | ||
| 95 | |||
| 96 | int | ||
| 97 | main() | ||
| 98 | { | ||
| 99 | auto data = read_file("data/day12.txt"); | ||
| 100 | part1(data); | ||
| 101 | } | ||
