aboutsummaryrefslogtreecommitdiff
path: root/src/day20.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2023-12-20 23:50:06 +0100
committerThomas Schmucker <ts@its1.de>2023-12-20 23:50:06 +0100
commit47bdba2c0a8dda2fb69da639a226e21911cfb651 (patch)
tree92bb1017377183360c9c71ea6f9c5acddf67454b /src/day20.cpp
parent240ab58b943a452e627c70c6ee9252955d89aa59 (diff)
downloadadvent-of-code-47bdba2c0a8dda2fb69da639a226e21911cfb651.tar.gz
advent-of-code-47bdba2c0a8dda2fb69da639a226e21911cfb651.tar.bz2
advent-of-code-47bdba2c0a8dda2fb69da639a226e21911cfb651.zip
Lösungen für Tag 20, Teil 2
Diffstat (limited to 'src/day20.cpp')
-rw-r--r--src/day20.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/day20.cpp b/src/day20.cpp
index 71b83a3..a17f2f2 100644
--- a/src/day20.cpp
+++ b/src/day20.cpp
@@ -3,6 +3,7 @@
3#include <iostream> 3#include <iostream>
4#include <map> 4#include <map>
5#include <memory> 5#include <memory>
6#include <numeric>
6#include <queue> 7#include <queue>
7#include <string> 8#include <string>
8#include <vector> 9#include <vector>
@@ -197,8 +198,97 @@ part1()
197 cout << sums[0] * sums[1] << endl; 198 cout << sums[0] * sums[1] << endl;
198} 199}
199 200
201void
202part2()
203{
204 queue<tuple<string, int, string>> queue;
205 map<string, shared_ptr<Module>> modules;
206
207 modules["output"] = make_shared<OutputModule>("output", queue);
208
209 const auto input = read_file("data/day20.txt");
210 for ( const auto& line: input ) {
211 const auto parts = split(line, " -> ");
212 auto module = parts[0];
213 const auto dests = split(parts[1], ", ");
214
215 shared_ptr<Module> ptr;
216 if ( module == "broadcaster" ) {
217 ptr = make_shared<BroadcasterModuler>(module, queue);
218 }
219 else if ( module.starts_with('%') ) {
220 module.erase(0, 1);
221 ptr = make_shared<FlipFlopModule>(module, queue);
222 }
223 else if ( module.starts_with('&') ) {
224 module.erase(0, 1);
225 ptr = make_shared<ConjunctionModule>(module, queue);
226 }
227 else {
228 cerr << "unknown module type: " << module << endl;
229 return;
230 }
231
232 for ( const auto& dest: dests ) {
233 ptr->add_link(dest);
234 }
235
236 modules[module] = ptr;
237 }
238
239 // register senders
240 string to_rx;
241 for ( auto& module: modules ) {
242 auto [name, ptr] = module;
243
244 for ( const auto& link: ptr->links_ ) {
245 if ( modules.contains(link) ) { // link == "rx"
246 modules[link]->register_sender(name);
247 }
248 else {
249 to_rx = name;
250 }
251 }
252 }
253
254 map<string, long> cycle_modules;
255 for ( auto& module: modules ) {
256 auto [name, ptr] = module;
257 const auto links = ptr->links_;
258
259 if ( find(links.begin(), links.end(), to_rx) != links.end() ) {
260 cycle_modules[name] = 0;
261 }
262 }
263
264 for ( long i = 1;; ++i ) {
265 queue.emplace("button", 0, "broadcaster");
266 while ( !queue.empty() ) {
267 const auto [sender, signal, receiver] = queue.front();
268 queue.pop();
269
270 if ( signal == 1 && cycle_modules.contains(sender) ) {
271 cycle_modules[sender] = i;
272 }
273
274 if ( modules.contains(receiver) ) {
275 modules[receiver]->trigger(sender, signal);
276 }
277 }
278 if ( all_of(cycle_modules.begin(), cycle_modules.end(), [](const auto& module) { return module.second != 0; }) ) {
279 long result = 1;
280 for ( const auto& module: cycle_modules ) {
281 result = lcm(result, module.second);
282 }
283 cout << result << endl;
284 break;
285 }
286 }
287}
288
200int 289int
201main() 290main()
202{ 291{
203 part1(); 292 part1();
293 part2();
204} 294}