diff --git a/2024/12/main-1.cpp b/2024/12/main-1.cpp new file mode 100644 index 0000000..4f9fd59 --- /dev/null +++ b/2024/12/main-1.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +vector>> squares; + +bool rec(int x, int y, set> ®ion) { + auto &[c, g] = squares[x][y]; + if (g) { + return false; + } + g = true; + region.insert({x, y}); + vector> checks = { + {x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}}; + for (auto [xx, yy] : checks) { + if (xx < 0 || yy < 0 || xx >= squares.size() || + yy >= squares[0].size()) { + continue; + } + if (get<0>(squares[xx][yy]) == c) { + rec(xx, yy, region); + } + } + return true; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + while (getline(cin, line) && !line.empty()) { + vector> l; + for (char c : line) { + l.push_back({c, false}); + } + squares.push_back(l); + } + + vector>> regions; + for (int x = 0; x < squares.size(); x++) { + for (int y = 0; y < squares[0].size(); y++) { + set> region; + if (rec(x, y, region)) { + regions.push_back(region); + } + } + } + + int total = 0; + for (auto ®ion : regions) { + int area = region.size(); + int perim = 0; + for (auto [x, y] : region) { + for (auto &d : vector>( + {{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}})) { + if (!region.contains(d)) { + perim++; + } + } + } + total += area * perim; + } + cout << total << '\n'; +} diff --git a/2024/12/main-2.cpp b/2024/12/main-2.cpp new file mode 100644 index 0000000..9ec1d5c --- /dev/null +++ b/2024/12/main-2.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +vector>> squares; + +bool rec(int x, int y, set> ®ion) { + auto &[c, g] = squares[x][y]; + if (g) { + return false; + } + g = true; + region.insert({x, y}); + vector> checks = { + {x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}}; + for (auto [xx, yy] : checks) { + if (xx < 0 || yy < 0 || xx >= squares.size() || + yy >= squares[0].size()) { + continue; + } + if (get<0>(squares[xx][yy]) == c) { + rec(xx, yy, region); + } + } + return true; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + while (getline(cin, line) && !line.empty()) { + vector> l; + for (char c : line) { + l.push_back({c, false}); + } + squares.push_back(l); + } + + vector>> regions; + for (int x = 0; x < squares.size(); x++) { + for (int y = 0; y < squares[0].size(); y++) { + set> region; + if (rec(x, y, region)) { + regions.push_back(region); + } + } + } + + int total = 0; + for (auto ®ion : regions) { + map, bool[4]> sides; + int area = region.size(); + for (auto [x, y] : region) { + vector> directions = { + {x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}}; + for (int i = 0; i < 4; i++) { + sides[{x, y}][i] = !region.contains(directions[i]); + } + } + int sidec = 0; + for (auto &[p, b] : sides) { + auto [x, y] = p; + for (int i = 0; i < 4; i++) { + if (!b[i]) { + continue; + } + pair other = + (i < 2) ? make_pair(x, y - 1) : make_pair(x - 1, y); + if (!sides.contains(other) || !sides[other][i]) { + sidec++; + } + } + } + total += area * sidec; + } + cout << total << '\n'; +}