#include #include const int MAXSIZE = 20; int square[MAXSIZE+2][MAXSIZE+2]; // Allow for border main() { int row, col; int N, M; ifstream in("day1b.dat", ios::nocreate); if(in == NULL) { cerr << "Error no input file\n"; exit(1); } in >> N; for(row = 1; row <= N; row++) for(col = 1; col <= N; col++) square[row][col] = 0; in >> M; // Number of holes int i; for(i = 0; i < M; i++) { // Read in coordinates of the hole in >> row; in >> col; if(row <= 0 || row > N || col <= 0 || col > N) { cerr << "Bad input data\n"; exit(1); } square[row][col] = 9; // 9 indicates mine // All the squares around it need to be incremented square[row-1][col-1]++; square[row-1][col]++; square[row-1][col+1]++; square[row][col-1]++; square[row][col+1]++; square[row+1][col-1]++; square[row+1][col]++; square[row+1][col+1]++; } // Initialise the array to hold the totals int total[10]; for(i = 0; i < 10; i++) total[i] = 0; // Update the totals for(row = 1; row <= N; row++) for(col = 1; col <= N; col++) if(square[row][col] <= 9) total[square[row][col]]++; else total[9]++; if(total[9] != M) // quick check. { cerr << "Uh Uh\n"; exit(1); } ofstream out("day1b.sol"); for(i = 8; i >= 1; i--) if(total[i] != 0) out << i << ' ' << total[i] << endl; return 0; }