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
|
#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;
}
vector<tuple<size_t, size_t>>
positions(size_t row, size_t col)
{
return {
{ row - 1, col - 1 },
{ row - 0, col - 1 },
{ row + 1, col - 1 },
{ row - 1, col },
{ row + 1, col },
{ row - 1, col + 1 },
{ row - 0, col + 1 },
{ row + 1, col + 1 },
};
}
void
part1() // NOLINT
{
auto lines = read_file("data/day03.txt");
auto is_symbol = [&lines](size_t row, size_t col) -> bool {
static const string symbols{ "*#+$%=-@&/" };
if ( row >= lines.size() || col >= lines[row].size() ) {
return false;
}
auto symbol = lines[row][col];
return symbols.find(symbol) != string::npos;
};
auto bordering = [&is_symbol](size_t row, size_t col) -> bool {
bool result = false;
for (const auto& position: positions(row, col)) {
result |= is_symbol(get<0>(position), get<1>(position));
}
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(row, col);
}
if ( result ) {
sum += stoi(line.substr(start, end - start));
}
start = end;
}
}
}
cout << sum << endl;
}
void
part2() // NOLINT
{
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];
if ( !is_digit(line[col]) ) {
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 ) {
vector<int> values;
auto lines_copy{ lines };
for ( const auto& position: positions(row, col) ) {
auto value = 0;
if ( find_integer(lines_copy, get<0>(position), get<1>(position), 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()
{
part1();
part2();
}
|