|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <utility> |
| 4 | +#include <vector> |
| 5 | +#include <unordered_map> |
| 6 | +#include <unordered_set> |
| 7 | +#include <set> |
| 8 | +#include <cmath> |
| 9 | +#include <algorithm> |
| 10 | + |
| 11 | + |
| 12 | +using Map = std::vector<std::vector<bool>>; |
| 13 | +std::ostream& operator<<(std::ostream& os, const std::pair<int, int>& c) { |
| 14 | + os << "(" << c.first << "," << c.second << ")"; |
| 15 | + return os; |
| 16 | +} |
| 17 | + |
| 18 | +template<class T> |
| 19 | +std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { |
| 20 | + for (const auto& x : v) os << x << " "; |
| 21 | + return os; |
| 22 | +} |
| 23 | + |
| 24 | +std::uint64_t part1(const Map& map) { |
| 25 | + int m = map.size(), n = map[0].size(); |
| 26 | + Map map1(m); |
| 27 | + for (int j = 0; j < n; ++j) { |
| 28 | + bool flag = true; |
| 29 | + for (int i = 0; i < m; ++i) { |
| 30 | + map1[i].push_back(map[i][j]); |
| 31 | + if (map[i][j]) flag = false; |
| 32 | + } |
| 33 | + if (flag) { |
| 34 | + for (int i = 0; i < m; ++i) map1[i].push_back(map[i][j]); |
| 35 | + } |
| 36 | + } |
| 37 | + Map map2; |
| 38 | + for (int i = 0; i < m; ++i) { |
| 39 | + map2.push_back(map1[i]); |
| 40 | + bool flag = true; |
| 41 | + for (const auto& b : map1[i]) { |
| 42 | + if (b) { |
| 43 | + flag = false; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + if (flag) map2.push_back(map1[i]); |
| 48 | + } |
| 49 | + m = map2.size(); |
| 50 | + n = map2[0].size(); |
| 51 | + std::vector<std::pair<int, int>> co; |
| 52 | + for (int i = 0; i < m; ++i) { |
| 53 | + for (int j = 0; j < n; ++j) { |
| 54 | + if (map2[i][j]) co.push_back({i, j}); |
| 55 | + } |
| 56 | + } |
| 57 | + std::uint64_t ans = 0; |
| 58 | + for (int i = 0; i < co.size() - 1; ++i) { |
| 59 | + for (int j = i + 1; j < co.size(); ++j) { |
| 60 | + auto diff_i = std::abs(co[i].first - co[j].first); |
| 61 | + auto diff_j = std::abs(co[i].second - co[j].second); |
| 62 | + ans += diff_i + diff_j; |
| 63 | + } |
| 64 | + } |
| 65 | + return ans; |
| 66 | +} |
| 67 | + |
| 68 | +std::uint64_t count(int l, int r, const std::vector<int>& v) { |
| 69 | + if (r < l) std::swap(l, r); |
| 70 | + auto bin = [&v](int x) { |
| 71 | + int i = 0, j = v.size() - 1; |
| 72 | + while (i < j) { |
| 73 | + auto mid = i + (j - i) / 2; |
| 74 | + if (v[mid] < x) i = mid + 1; |
| 75 | + else j = mid; |
| 76 | + } |
| 77 | + if (x > v[j]) return j + 1; |
| 78 | + return j; |
| 79 | + }; |
| 80 | + return bin(r) - bin(l); |
| 81 | +} |
| 82 | + |
| 83 | +std::uint64_t part2(const Map& map) { |
| 84 | + constexpr std::uint64_t factor = 1'000'000; |
| 85 | + int m = map.size(), n = map[0].size(); |
| 86 | + std::vector<int> is, js; |
| 87 | + for (int i = 0; i < m; ++i) { |
| 88 | + bool flag = true; |
| 89 | + for (int j = 0; j < n; ++j) { |
| 90 | + if (map[i][j]) { |
| 91 | + flag = false; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + if (flag) is.push_back(i); |
| 96 | + } |
| 97 | + for (int j = 0; j < n; ++j) { |
| 98 | + bool flag = true; |
| 99 | + for (int i = 0; i < n; ++i) { |
| 100 | + if (map[i][j]) { |
| 101 | + flag = false; |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + if (flag) js.push_back(j); |
| 106 | + } |
| 107 | + std::vector<std::pair<int, int>> co; |
| 108 | + for (int i = 0; i < m; ++i) { |
| 109 | + for (int j = 0; j < n; ++j) { |
| 110 | + if (map[i][j]) co.push_back({i, j}); |
| 111 | + } |
| 112 | + } |
| 113 | + // std::cout << is << std::endl; |
| 114 | + // std::cout << js << std::endl; |
| 115 | + // std::cout << co << std::endl; |
| 116 | + std::uint64_t ans = 0; |
| 117 | + for (int i = 0; i < co.size() - 1; ++i) { |
| 118 | + for (int j = i + 1; j < co.size(); ++j) { |
| 119 | + // std::cout << co[i] << " " << co[j] << std::endl; |
| 120 | + auto cnt_i = count(co[i].first, co[j].first, is); |
| 121 | + auto cnt_j = count(co[i].second, co[j].second, js); |
| 122 | + // std::cout << count(co[i].first, co[j].first, is) << std::endl; |
| 123 | + // std::cout << count(co[i].second, co[j].second, js) << std::endl; |
| 124 | + auto diff_i = std::abs(co[i].first - co[j].first); |
| 125 | + auto diff_j = std::abs(co[i].second - co[j].second); |
| 126 | + ans += diff_i + diff_j - cnt_i - cnt_j + factor * cnt_i + factor * cnt_j; |
| 127 | + } |
| 128 | + } |
| 129 | + return ans; |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +int main() { |
| 134 | + std::ifstream f("11.txt"); |
| 135 | + std::string line; |
| 136 | + Map map; |
| 137 | + while (std::getline(f, line)) { |
| 138 | + map.push_back({}); |
| 139 | + for (const auto& c : line) { |
| 140 | + if (c == '.') map.back().push_back(0); |
| 141 | + else map.back().push_back(1); |
| 142 | + } |
| 143 | + } |
| 144 | + std::cout << part2(map) << std::endl; |
| 145 | +} |
| 146 | + |
| 147 | + |
0 commit comments