diff options
Diffstat (limited to '2024')
| -rw-r--r-- | 2024/src/day14.cpp | 63 |
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 | ||
| 171 | constexpr vector<pos_type> | ||
| 172 | neighbors(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 | |||
| 178 | size_t | ||
| 179 | flood_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 | |||
| 215 | void | ||
| 216 | part2_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 | |||
| 169 | int | 231 | int |
| 170 | main() | 232 | main() |
| 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 | } |
