#include #include using namespace std; const vector SYMBOLS = {'#', '$', '%', '&', '+', '-', '/', '=', '@', '*'}; const vector NUMBERS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; int main() { vector lines; for (string line; getline(cin, line);) lines.push_back(line); size_t rows = lines.size(); size_t cols = lines[0].size(); int output = 0; auto check_and_mark = [lines, cols](vector> &checked, unsigned int row, unsigned int col) -> int { int number = 0; if (lines[row][col] < '0' || lines[row][col] > '9') return number; int begin_col = col; while (true) { if ((begin_col - 1) < 0) break; if (lines[row][begin_col - 1] >= '0' && lines[row][begin_col - 1] <= '9') begin_col--; else break; } for (col = begin_col; lines[row][col] >= '0' && lines[row][col] <= '9' && col < cols; col++) { number *= 10; number += lines[row][col] - '0'; checked[row][col] = true; } printf("%d at beginning coords %c%d\n", number, "ABCDEFGHIJKLMNOP"[begin_col], row +1); return number; }; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { for (char symbol : SYMBOLS) { if (lines[i][j] == symbol) { int temp_cogs_mul = 1; unsigned int temp_cogs_cnt = 0; vector> checked(rows, vector(cols, false)); printf("[%c at %c%d]\n", lines[i][j], "ABCDEFGHIJKLMNOP"[j], i +1); 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; if (checked[y][x] == true) continue; int number = check_and_mark(checked, y, x); if (number > 0 && lines[i][j] == '*') { temp_cogs_mul *= number; temp_cogs_cnt++; } } } if (temp_cogs_cnt == 2) { printf("cog count 2, adding ratio %d\n", temp_cogs_mul); output += temp_cogs_mul; } } } } } printf(">> %d\n", output); return 0; }