ErnestGalbrun

Untitled

Dec 3rd, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <range/v3/all.hpp> // get everything
  4. #include <absl/time/time.h>
  5. #include <absl/time/clock.h>
  6. #include <sstream>
  7. #include <map>
  8. #include <unordered_set>
  9.  
  10. //using namespace std;
  11. using namespace ::ranges;
  12. using namespace ::ranges::views;
  13.  
  14. int main() {
  15.     auto start = absl::Now();
  16.  
  17.     std::unordered_multiset<std::string> lines;
  18.     for (std::string line; std::getline(std::cin, line);) {
  19.         if (line.empty()) break;
  20.         lines.insert(line);
  21.     }
  22.  
  23.     auto filter_until = [](auto lines, bool keep_most) {
  24.         for (int i = 0; lines.size() > 1; ++i) {
  25.             auto n = count_if(lines, [i](const auto &l) { return l[i] == '1'; });
  26.             bool more_ones = n >= lines.size() - n;
  27.             lines = lines | filter([i, more_ones, keep_most](const std::string &l) {
  28.                 return more_ones ^ keep_most ? l[i] == '1' : l[i] == '0';
  29.             });
  30.         }
  31.         return lines;
  32.     };
  33.  
  34.     auto ox = filter_until(lines, true);
  35.     auto co2 = filter_until(lines, false);
  36.  
  37.     std::cout << stoi(*ox.begin(), nullptr, 2) * stoi(*co2.begin(), nullptr, 2) << std::endl;
  38.     std::cout << absl::Now() - start << std::endl;
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment