summaryrefslogtreecommitdiff
path: root/03/main.cc
blob: 64e6748f8691df056671a403e964f8a0f1f359b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
#include <vector>
using namespace std;

const vector<char> SYMBOLS = {'#', '$', '%', '&', '+', '-', '/', '=', '@', '*'};
const vector<char> NUMBERS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

int main() {
	vector<string> 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<vector<bool>> &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<vector<bool>> checked(rows, vector<bool>(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;
}