aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-12-14 13:39:07 +0100
committerThomas Schmucker <ts@its1.de>2024-12-14 13:39:07 +0100
commit5a17843904394e73f5ae7001dd5f22b7491f6bb9 (patch)
tree711e0456ff743e37a1cd8b0e3e9d9f428c03946d /2024
parentfc3cc642c620221cafed36c8c07da34bfb566843 (diff)
downloadadvent-of-code-5a17843904394e73f5ae7001dd5f22b7491f6bb9.tar.gz
advent-of-code-5a17843904394e73f5ae7001dd5f22b7491f6bb9.tar.bz2
advent-of-code-5a17843904394e73f5ae7001dd5f22b7491f6bb9.zip
aoc 2024, day 14, flood fill :)
Diffstat (limited to '2024')
-rw-r--r--2024/src/day14.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/2024/src/day14.cpp b/2024/src/day14.cpp
index 97e5fab..aa014e6 100644
--- a/2024/src/day14.cpp
+++ b/2024/src/day14.cpp
@@ -1,6 +1,8 @@
1#include <cstddef>
1#include <fstream> 2#include <fstream>
2#include <iostream> 3#include <iostream>
3#include <map> 4#include <map>
5#include <queue>
4#include <regex> 6#include <regex>
5#include <set> 7#include <set>
6#include <tuple> 8#include <tuple>
@@ -166,6 +168,66 @@ part2_alternative(vector<tuple<pos_type, velo_type>> robots, long width, long he
166 } 168 }
167} 169}
168 170
171constexpr vector<pos_type>
172neighbors(pos_type pos)
173{
174 const auto [x, y] = pos;
175 return { { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } };
176}
177
178size_t
179flood_fill(const vector<tuple<pos_type, velo_type>>& robots)
180{
181 set<pos_type> positions;
182 for ( const auto& [pos, velo]: robots ) {
183 positions.insert(pos);
184 }
185
186 size_t max_seen = 0;
187 for ( const auto& start: positions ) {
188 set<pos_type> seen;
189
190 queue<pos_type> queue;
191 queue.push(start);
192
193 while ( !queue.empty() ) {
194 auto pos = queue.front();
195 queue.pop();
196
197 if ( seen.contains(pos) ) {
198 continue;
199 }
200
201 seen.insert(pos);
202
203 for ( const auto& neighbor: neighbors(pos) ) {
204 if ( positions.contains(neighbor) ) {
205 queue.push(neighbor);
206 }
207 }
208 }
209 max_seen = max(max_seen, seen.size());
210 }
211
212 return max_seen;
213}
214
215void
216part2_flood_fill(vector<tuple<pos_type, velo_type>> robots, long width, long height)
217{
218 size_t max_count = 0;
219 long second_found = 0;
220 for ( long second = 0; second != 100000; ++second ) {
221 auto count = flood_fill(robots);
222 if ( count > max_count ) {
223 max_count = count;
224 second_found = second;
225 }
226 move(robots, width, height);
227 }
228 cout << second_found << endl;
229}
230
169int 231int
170main() 232main()
171{ 233{
@@ -177,5 +239,6 @@ main()
177 part1(robots, 101, 103); 239 part1(robots, 101, 103);
178 part2(robots, 101, 103); 240 part2(robots, 101, 103);
179 part2_alternative(robots, 101, 103); 241 part2_alternative(robots, 101, 103);
242 part2_flood_fill(robots, 101, 103);
180#endif 243#endif
181} 244}