#include #include #include using namespace std; constexpr int grid_size{10}; int grid[grid_size][grid_size] = {0}; void output_grid(); std::ostream& bold_on(std::ostream& os); std::ostream& bold_off(std::ostream& os); int main(int argc, char* argv[]) { string line = ""; for (int y = 0; y < grid_size; y++) { getline(cin, line); for (int x = 0; x < grid_size; x++) { grid[x][y] = (int)(line[x]-'0'); } } int t = 0; bool all_flashed = false; while(!all_flashed) { all_flashed = true; //Step 1, increment for (int y = 0; y < grid_size; y++) { for (int x = 0; x < grid_size; x++) { ++grid[x][y]; } } //Step 2, flash bool flashed[grid_size][grid_size] = {false}; bool flash_occurred = false; do { flash_occurred = false; for (int y = 0; y < grid_size; y++) { for (int x = 0; x < grid_size; x++) { if (!flashed[x][y] && grid[x][y] > 9) { flashed[x][y] = true; flash_occurred = true; for(int fy = max(0,y-1); fy < min(grid_size, y+2); fy++) { for(int fx = max(0,x-1); fx < min(grid_size,x+2); fx++) { grid[fx][fy]++; } } } } } } while (flash_occurred); //Step 3, flashed are cleared for (int y = 0; y < grid_size; y++) { for (int x = 0; x < grid_size; x++) { if (flashed[x][y]) { grid[x][y] = 0; } else { all_flashed = false; } } } t++; } cout << t << endl; } void output_grid() { for (int y = 0; y < grid_size; y++) { for (int x = 0; x < grid_size; x++) { if (0 == grid[x][y]) { cout << bold_on << grid[x][y] << bold_off; } else { cout << grid[x][y]; } } cout << endl; } cout << endl; } std::ostream& bold_on(std::ostream& os) { return os << "\e[1m"; } std::ostream& bold_off(std::ostream& os) { return os << "\e[0m"; }