ErnestGalbrun

Untitled

Dec 4th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 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. #include <absl/strings/str_split.h>
  10. #include <absl/algorithm/container.h>
  11.  
  12. using ::ranges::views::transform;
  13. using ::ranges::views::iota;
  14. using ::ranges::views::split;
  15. using ::ranges::views::join;
  16. using ::ranges::views::drop;
  17. using ::ranges::views::filter;
  18. using ::ranges::views::replace;
  19. using ::ranges::views::stride;
  20. using ::ranges::accumulate;
  21. using ::ranges::to;
  22. using ::ranges::count;
  23. using ::ranges::any_of;
  24. using ::ranges::all_of;
  25. using ::std::vector;
  26. using ::std::cin;
  27. using ::std::cout;
  28. using ::std::endl;
  29. using ::std::string;
  30. using ::std::not_fn;
  31.  
  32. int main() {
  33.     auto start = absl::Now();
  34.     string l;
  35.     getline(std::cin, l);
  36.     auto plays = l
  37.                  | split(',')
  38.                  | transform([](auto const &rc) {
  39.         string s = rc | to<std::string>;
  40.         return atoi(s.c_str());
  41.     }) | to<vector<int>>;
  42.  
  43.     // Discard empty first line.
  44.     getline(cin, l);
  45.  
  46.     vector<string> lines;
  47.     string line;
  48.     while (getline(cin, line)) {
  49.         lines.push_back(line);
  50.     }
  51.  
  52.     auto string_boards = lines
  53.                          | split("")
  54.                          | to<vector<vector<string>>>;
  55.  
  56.     auto boards = string_boards
  57.                   | transform([](auto &board_lines) {
  58.         std::stringstream ss(board_lines | join(' ') | to<string>);
  59.         return ::ranges::istream_view<int>(ss) | to<vector<int>>;
  60.     }) | to<vector<vector<int>>>;
  61.  
  62.     int K = sqrt(boards[0].size());
  63.  
  64.     auto rows = [K](auto &board) {
  65.         return board | ::ranges::views::chunk(K);
  66.     };
  67.  
  68.     auto columns = [K](auto &board) {
  69.         return ranges::views::iota(0, K) | transform([board, K](int offset) {
  70.             return board | drop(offset) | stride(K);
  71.         });
  72.     };
  73.  
  74.     auto row_columns = [&rows, &columns](auto &board) {
  75.         return ::ranges::views::concat(rows(board), columns(board));
  76.     };
  77.  
  78.     auto is_marked = [](int x) { return x == -1; };
  79.  
  80.     int lastScore = 0;
  81.  
  82.     auto isWinning = [rows, is_marked, columns, &lastScore](const auto &board, int n) {
  83.         bool won = any_of(rows(board), [is_marked](const auto &r) {
  84.             return all_of(r, is_marked);
  85.         }) || any_of(columns(board), [is_marked](const auto &r) {
  86.             return all_of(r, is_marked);
  87.         });
  88.         if (won) {
  89.             lastScore = n * accumulate(board | filter(not_fn(is_marked)), 0);
  90.         }
  91.         return won;
  92.     };
  93.  
  94.     for (auto n: plays) {
  95.         boards = boards
  96.                  | transform([n](auto &b) { return b | replace(n, -1); })
  97.                  | filter([n, isWinning](const auto &b) {
  98.             return !isWinning(b, n);
  99.         })
  100.                  | to<vector<vector<int>>>;
  101.         if (boards.empty()) {
  102.             break;
  103.         }
  104.     }
  105.  
  106.     cout << lastScore << endl;
  107.  
  108.     cout << absl::Now() - start << endl;
  109.     return 0;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment