112 lines
2.5 KiB
C++
112 lines
2.5 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <algorithm>
|
|
|
|
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 total_flashes = 0;
|
|
for (int t = 0; t < 100; t++)
|
|
{
|
|
//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;
|
|
++total_flashes;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
cout << total_flashes << 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";
|
|
} |