aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2023-12-03 15:16:24 +0100
committerThomas Schmucker <ts@its1.de>2023-12-03 15:16:24 +0100
commit5708a617b10e328c499da837d764a51e98d8bcc0 (patch)
tree7a8f6211eece341338c1917990b4cf7f14b1cd1a /src
parentfc4fbbc41d275a187d026042eb88a92153ccaa80 (diff)
downloadadvent-of-code-5708a617b10e328c499da837d764a51e98d8bcc0.tar.gz
advent-of-code-5708a617b10e328c499da837d764a51e98d8bcc0.tar.bz2
advent-of-code-5708a617b10e328c499da837d764a51e98d8bcc0.zip
Lösung für Tag3
Diffstat (limited to 'src')
-rw-r--r--src/day03.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/day03.cpp b/src/day03.cpp
new file mode 100644
index 0000000..d5e3fff
--- /dev/null
+++ b/src/day03.cpp
@@ -0,0 +1,187 @@
1#include <cctype>
2#include <fstream>
3#include <iostream>
4#include <string>
5#include <string_view>
6#include <vector>
7
8using namespace std;
9
10vector<string>
11read_file(string_view filename)
12{
13 fstream input{ filename };
14 vector<string> data;
15
16 for ( string line; getline(input, line); ) {
17 data.emplace_back(line);
18 }
19
20 return data;
21}
22
23bool
24is_digit(char chr)
25{
26 return std::isdigit(static_cast<unsigned char>(chr)) != 0;
27}
28
29void
30part1() // NOLINT
31{
32 auto lines = read_file("data/day03.txt");
33
34 auto is_symbol = [&lines](int row, int col) -> bool {
35 static const string symbols{ "*#+$%=-@&/" };
36
37 if ( row < 0 || col < 0 ) {
38 return false;
39 }
40 if ( size_t(row) >= lines.size() || size_t(col) >= lines[size_t(row)].size() ) {
41 return false;
42 }
43
44 auto symbol = lines[size_t(row)][size_t(col)];
45
46 return symbols.find(symbol) != string::npos;
47 };
48
49 auto bordering = [&is_symbol](int row, int col) -> bool {
50 bool result = false;
51
52 result |= is_symbol(row - 1, col - 1);
53 result |= is_symbol(row - 0, col - 1);
54 result |= is_symbol(row + 1, col - 1);
55
56 result |= is_symbol(row - 1, col);
57 result |= is_symbol(row + 1, col);
58
59 result |= is_symbol(row - 1, col + 1);
60 result |= is_symbol(row - 0, col + 1);
61 result |= is_symbol(row + 1, col + 1);
62
63 return result;
64 };
65
66 auto sum = 0;
67 for ( size_t row = 0; row != lines.size(); ++row ) {
68 const auto& line = lines[row];
69
70 string::size_type start = 0;
71 while ( start != line.size() ) {
72 while ( start != line.size() && !is_digit(line[start]) ) {
73 ++start;
74 }
75
76 auto end = start;
77 while ( end != line.size() && is_digit(line[end]) ) {
78 ++end;
79 }
80
81 if ( start != end ) {
82 auto result = false;
83
84 for ( auto col = start; col != end; ++col ) {
85 result |= bordering(int(row), int(col));
86 }
87
88 if ( result ) {
89 sum += stoi(line.substr(start, end - start));
90 }
91
92 start = end;
93 }
94 }
95 }
96 cout << sum << endl;
97}
98
99void
100part2()
101{
102 auto lines = read_file("data/day03.txt");
103
104 auto find_integer = [](vector<string>& lines, size_t row, size_t col, int& value) -> bool { // NOLINT
105 if ( row >= lines.size() || col >= lines[row].size() ) {
106 return false;
107 }
108
109 auto& line = lines[row];
110
111 auto digit = line[col];
112
113 if ( !is_digit(digit) ) {
114 return false;
115 }
116
117 // Anfang der Zahl suchen
118 auto start = col;
119 for ( ; start != 0 && is_digit(line[start - 1]); --start ) // NOLINT
120 ;
121
122 auto end = col;
123 for ( ; end != line.size() && is_digit(line[end]); ++end ) // NOLINT
124 ;
125
126 value = stoi(line.substr(start, end - start));
127
128 for ( ; start != end; ++start ) {
129 line[start] = '.';
130 }
131
132 return true;
133 };
134
135 auto sum = 0;
136 for ( size_t row = 0; row != lines.size(); ++row ) {
137 const auto& line = lines[row];
138
139 auto col = line.find('*');
140 while ( col != string::npos ) {
141 int value = 0;
142 vector<int> values;
143 auto lines_copy{ lines };
144
145 if ( find_integer(lines_copy, row - 1, col - 1, value) ) {
146 values.emplace_back(value);
147 }
148 if ( find_integer(lines_copy, row - 0, col - 1, value) ) {
149 values.emplace_back(value);
150 }
151 if ( find_integer(lines_copy, row + 1, col - 1, value) ) {
152 values.emplace_back(value);
153 }
154
155 if ( find_integer(lines_copy, row - 1, col, value) ) {
156 values.emplace_back(value);
157 }
158 if ( find_integer(lines_copy, row + 1, col, value) ) {
159 values.emplace_back(value);
160 }
161
162 if ( find_integer(lines_copy, row - 1, col + 1, value) ) {
163 values.emplace_back(value);
164 }
165 if ( find_integer(lines_copy, row - 0, col + 1, value) ) {
166 values.emplace_back(value);
167 }
168 if ( find_integer(lines_copy, row + 1, col + 1, value) ) {
169 values.emplace_back(value);
170 }
171
172 if ( values.size() == 2 ) {
173 auto ratio = values[0] * values[1];
174 sum += ratio;
175 }
176
177 col = line.find('*', col + 1);
178 }
179 }
180 cout << sum << endl;
181}
182
183int
184main()
185{
186 part2();
187}