#include using namespace std; string FILE_NAME = "4.txt"; struct Card { map> numbers = {}; array, 5> opens; Card(){ for(int x = 0; x < 5; ++x){ opens[x].fill(true); } } bool check(pair coords){ bool x_result = true, y_result = true; for(int x = 0; x < 5; ++x){ if(opens[x][coords.second]){ x_result = false; break; } } for(int y = 0; y < 5; ++y){ if(opens[coords.first][y]){ y_result = false; break; } } return x_result || y_result; } void insert(int n, pair coords){ numbers[n] = coords; } int score(){ int result = 0; for(auto n : numbers){ auto coords = n.second; if(opens[coords.first][coords.second]){ result += n.first; } } return result; } bool call(int n){ bool result = false; if(numbers.find(n) != numbers.end()){ auto coords = numbers[n]; opens[coords.first][coords.second] = false; result = check(coords); } return result; } }; vector string_to_int_vector(const string& s){ vector result; int carry = 0; for(auto c : s){ if(c >= 48 && c <= 57){ carry *= 10; carry += c - 48; } else{ result.push_back(carry); carry = 0; } } result.push_back(carry); return result; } int main(){ ios::sync_with_stdio(0); cin.tie(0); ifstream file_stream; file_stream.open(FILE_NAME); if(!file_stream.is_open()){ cerr << "File " << FILE_NAME << " not opened correctly\n"; return 0; } // the first line will always be the sequence of bingo calls string line_1; getline(file_stream, line_1); auto calls = string_to_int_vector(line_1); // The second line will always be empty and not parsed or whatever getline(file_stream, line_1); vector bingo_cards; string cur_line; while(getline(file_stream, cur_line)){ Card card; for(int i = 0; i < 5; ++i){ istringstream ss(cur_line); for(int j = 0; j < 5; ++j){ int a; ss >> a; card.insert(a, {i, j}); } getline(file_stream, cur_line); } bingo_cards.push_back(card); } for(auto n : calls){ for(auto& board: bingo_cards){ bool r = board.call(n); if(r){ cout << n << " * " << board.score() << " = " << n * board.score() << '\n'; return 0; } } } cout << flush; file_stream.close(); return 0; }