aboutsummaryrefslogtreecommitdiff
path: root/src/day03.cpp
blob: d5e3fff845c2a424a4dfe4398a58d736aa476a73 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

using namespace std;

vector<string>
read_file(string_view filename)
{
	fstream        input{ filename };
	vector<string> data;

	for ( string line; getline(input, line); ) {
		data.emplace_back(line);
	}

	return data;
}

bool
is_digit(char chr)
{
	return std::isdigit(static_cast<unsigned char>(chr)) != 0;
}

void
part1() // NOLINT
{
	auto lines = read_file("data/day03.txt");

	auto is_symbol = [&lines](int row, int col) -> bool {
		static const string symbols{ "*#+$%=-@&/" };

		if ( row < 0 || col < 0 ) {
			return false;
		}
		if ( size_t(row) >= lines.size() || size_t(col) >= lines[size_t(row)].size() ) {
			return false;
		}

		auto symbol = lines[size_t(row)][size_t(col)];

		return symbols.find(symbol) != string::npos;
	};

	auto bordering = [&is_symbol](int row, int col) -> bool {
		bool result = false;

		result |= is_symbol(row - 1, col - 1);
		result |= is_symbol(row - 0, col - 1);
		result |= is_symbol(row + 1, col - 1);

		result |= is_symbol(row - 1, col);
		result |= is_symbol(row + 1, col);

		result |= is_symbol(row - 1, col + 1);
		result |= is_symbol(row - 0, col + 1);
		result |= is_symbol(row + 1, col + 1);

		return result;
	};

	auto sum = 0;
	for ( size_t row = 0; row != lines.size(); ++row ) {
		const auto& line = lines[row];

		string::size_type start = 0;
		while ( start != line.size() ) {
			while ( start != line.size() && !is_digit(line[start]) ) {
				++start;
			}

			auto end = start;
			while ( end != line.size() && is_digit(line[end]) ) {
				++end;
			}

			if ( start != end ) {
				auto result = false;

				for ( auto col = start; col != end; ++col ) {
					result |= bordering(int(row), int(col));
				}

				if ( result ) {
					sum += stoi(line.substr(start, end - start));
				}

				start = end;
			}
		}
	}
	cout << sum << endl;
}

void
part2()
{
	auto lines = read_file("data/day03.txt");

	auto find_integer = [](vector<string>& lines, size_t row, size_t col, int& value) -> bool { // NOLINT
		if ( row >= lines.size() || col >= lines[row].size() ) {
			return false;
		}

		auto& line = lines[row];

		auto digit = line[col];

		if ( !is_digit(digit) ) {
			return false;
		}

		// Anfang der Zahl suchen
		auto start = col;
		for ( ; start != 0 && is_digit(line[start - 1]); --start ) // NOLINT
			;

		auto end = col;
		for ( ; end != line.size() && is_digit(line[end]); ++end ) // NOLINT
			;

		value = stoi(line.substr(start, end - start));

		for ( ; start != end; ++start ) {
			line[start] = '.';
		}

		return true;
	};

	auto sum = 0;
	for ( size_t row = 0; row != lines.size(); ++row ) {
		const auto& line = lines[row];

		auto col = line.find('*');
		while ( col != string::npos ) {
			int value = 0;
			vector<int> values;
			auto lines_copy{ lines };

			if ( find_integer(lines_copy, row - 1, col - 1, value) ) {
				values.emplace_back(value);
			}
			if ( find_integer(lines_copy, row - 0, col - 1, value) ) {
				values.emplace_back(value);
			}
			if ( find_integer(lines_copy, row + 1, col - 1, value) ) {
				values.emplace_back(value);
			}

			if ( find_integer(lines_copy, row - 1, col, value) ) {
				values.emplace_back(value);
			}
			if ( find_integer(lines_copy, row + 1, col, value) ) {
				values.emplace_back(value);
			}

			if ( find_integer(lines_copy, row - 1, col + 1, value) ) {
				values.emplace_back(value);
			}
			if ( find_integer(lines_copy, row - 0, col + 1, value) ) {
				values.emplace_back(value);
			}
			if ( find_integer(lines_copy, row + 1, col + 1, value) ) {
				values.emplace_back(value);
			}

			if ( values.size() == 2 ) {
				auto ratio = values[0] * values[1];
				sum += ratio;
			}

			col = line.find('*', col + 1);
		}
	}
	cout << sum << endl;
}

int
main()
{
	part2();
}