Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <range/v3/all.hpp> // get everything
- #include <absl/time/time.h>
- #include <absl/time/clock.h>
- #include <sstream>
- #include <map>
- #include <unordered_set>
- #include <absl/strings/str_split.h>
- #include <absl/algorithm/container.h>
- using ::ranges::views::transform;
- using ::ranges::views::iota;
- using ::ranges::views::split;
- using ::ranges::views::join;
- using ::ranges::views::drop;
- using ::ranges::views::filter;
- using ::ranges::views::replace;
- using ::ranges::views::stride;
- using ::ranges::accumulate;
- using ::ranges::to;
- using ::ranges::count;
- using ::ranges::any_of;
- using ::ranges::all_of;
- using ::std::vector;
- using ::std::cin;
- using ::std::cout;
- using ::std::endl;
- using ::std::string;
- using ::std::not_fn;
- int main() {
- auto start = absl::Now();
- string l;
- getline(std::cin, l);
- auto plays = l
- | split(',')
- | transform([](auto const &rc) {
- string s = rc | to<std::string>;
- return atoi(s.c_str());
- }) | to<vector<int>>;
- // Discard empty first line.
- getline(cin, l);
- vector<string> lines;
- string line;
- while (getline(cin, line)) {
- lines.push_back(line);
- }
- auto string_boards = lines
- | split("")
- | to<vector<vector<string>>>;
- auto boards = string_boards
- | transform([](auto &board_lines) {
- std::stringstream ss(board_lines | join(' ') | to<string>);
- return ::ranges::istream_view<int>(ss) | to<vector<int>>;
- }) | to<vector<vector<int>>>;
- int K = sqrt(boards[0].size());
- auto rows = [K](auto &board) {
- return board | ::ranges::views::chunk(K);
- };
- auto columns = [K](auto &board) {
- return ranges::views::iota(0, K) | transform([board, K](int offset) {
- return board | drop(offset) | stride(K);
- });
- };
- auto row_columns = [&rows, &columns](auto &board) {
- return ::ranges::views::concat(rows(board), columns(board));
- };
- auto is_marked = [](int x) { return x == -1; };
- int lastScore = 0;
- auto isWinning = [rows, is_marked, columns, &lastScore](const auto &board, int n) {
- bool won = any_of(rows(board), [is_marked](const auto &r) {
- return all_of(r, is_marked);
- }) || any_of(columns(board), [is_marked](const auto &r) {
- return all_of(r, is_marked);
- });
- if (won) {
- lastScore = n * accumulate(board | filter(not_fn(is_marked)), 0);
- }
- return won;
- };
- for (auto n: plays) {
- boards = boards
- | transform([n](auto &b) { return b | replace(n, -1); })
- | filter([n, isWinning](const auto &b) {
- return !isWinning(b, n);
- })
- | to<vector<vector<int>>>;
- if (boards.empty()) {
- break;
- }
- }
- cout << lastScore << endl;
- cout << absl::Now() - start << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment