From e275c8c30af198c17a84d0ac288154df51282b77 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 3 Dec 2023 16:47:51 +0100 Subject: garbage man day 3 complete --- 03/main.cc | 117 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 55 insertions(+), 62 deletions(-) diff --git a/03/main.cc b/03/main.cc index b452c95..64e6748 100644 --- a/03/main.cc +++ b/03/main.cc @@ -1,86 +1,79 @@ #include +#include using namespace std; -const vector SYMBOLS = {'#', '$', '%', '&', '*', '+', '-', '/', '=', '@'}; +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; + int output = 0; - for (char symbol : SYMBOLS) - if (lines[y][x] == symbol) - valid_mask[i][j] = true; - } - } + 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 (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"); - } + 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; + }; - // 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; + 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; + } + } } } - 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); -- cgit v1.2.3