#include using namespace std; const vector SYMBOLS = {'#', '$', '%', '&', '*', '+', '-', '/', '=', '@'}; const vector NUMBERS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; int main() { // phase 1 -- read lines into buffer vector lines; for (string line; getline(cin, line);) lines.push_back(line); size_t rows = lines.size(); size_t cols = lines[0].size(); // phase 2 -- create valid number mask vector> valid_mask(rows, vector(cols, false)); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { // row and column offset for checking diagonals too for (int roff = -1; roff <= 1; roff++) { for (int coff = -1; coff <= 1; coff++) { // skip any x,y coordinates that are outide the "grid" int y = i + roff; if (y < 0) continue; if (y >= rows) continue; int x = j + coff; if (x < 0) continue; if (x >= cols) continue; for (char symbol : SYMBOLS) if (lines[y][x] == symbol) valid_mask[i][j] = true; } } } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) printf("%c", lines[i][j]); printf("\n"); } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) printf("%c", valid_mask[i][j] ? 'x' : ' '); printf("\n"); } // phase 3 -- read each number and add to output if any of its digits are // true in valid_mask int output = 0; int digit = 0; int valid_digit = false; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { char cell = lines[i][j]; bool valid = valid_mask[i][j]; int last_digit = digit; for (char num : NUMBERS) { if (cell != num) continue; // printf("[%c]", cell); digit *= 10; digit += cell - '0'; if (valid) valid_digit = true; } if (digit == last_digit && digit != 0) { // printf(" (inline, %d, %s)\n", digit, valid_digit ? "valid" : "not valid"); if (valid_digit) output += digit; digit = 0; valid_digit = false; } } if (digit != 0) { // printf(" (EOL, %d, %s)\n", digit, valid_digit ? "valid" : "not valid"); if (valid_digit) output += digit; digit = 0; valid_digit = false; } } printf(">> %d\n", output); return 0; }