aboutsummaryrefslogtreecommitdiff
path: root/src/2018/day7/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-20 15:12:16 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-20 15:12:16 +0800
commitcf2fa8493234e8f66468793d9b4a7633c3704a14 (patch)
tree582ff4ddb498efb42960bed3c8e14e80e299d0d9 /src/2018/day7/aoc.cpp
parent787eb42f24cda88adfd0c041c08110581c0e11e1 (diff)
downloadadvent-of-code-cf2fa8493234e8f66468793d9b4a7633c3704a14.tar.gz
advent-of-code-cf2fa8493234e8f66468793d9b4a7633c3704a14.zip
2018 day7
Diffstat (limited to 'src/2018/day7/aoc.cpp')
-rw-r--r--src/2018/day7/aoc.cpp39
1 files changed, 26 insertions, 13 deletions
diff --git a/src/2018/day7/aoc.cpp b/src/2018/day7/aoc.cpp
index 354bfc8..6d6a723 100644
--- a/src/2018/day7/aoc.cpp
+++ b/src/2018/day7/aoc.cpp
@@ -1,9 +1,20 @@
#include "aoc.h"
+#include <algorithm>
namespace aoc2018 {
std::map<char, instruction*> instruction::instructions = {};
size_t instruction::done_total = 0;
+void print() {
+ for (auto& kv : instruction::instructions) {
+ std::cout << kv.second->label << " -> ";
+ for (auto x : kv.second->deps) {
+ std::cout << x->label << " ";
+ }
+ std::cout << std::endl;
+ }
+}
+
int day7(line_view file, char sequence[]) {
per_line(file, [](line_view lv) {
char p1 = *(lv.line + 5);
@@ -22,18 +33,22 @@ int day7(line_view file, char sequence[]) {
n = instruction::next();
}
- auto check = [](int s, instruction** w) {
- if (*w != nullptr && (*w)->finished(s)) {
- (*w)->make_done();
- // std::cout << (*w)->label << " finishes at " << s << std::endl;
- *w = nullptr;
+ // print();
+
+ auto check = [](int s, instruction** w, size_t size) {
+ for (size_t i = 0; i < size; i++) {
+ if (w[i] != nullptr && w[i]->finished(s)) {
+ (w[i])->make_done();
+ w[i] = nullptr;
+ }
}
- if (*w == nullptr) {
- instruction::next(w);
- if (*w != nullptr) {
- // std::cout << (*w)->label << " starts at " << s << std::endl;
- (*w)->begin(s);
+ for (size_t i = 0; i < size; i++) {
+ if (w[i] == nullptr) {
+ instruction::next(&w[i]);
+ if (w[i] != nullptr) {
+ w[i]->begin(s);
+ }
}
}
};
@@ -44,9 +59,7 @@ int day7(line_view file, char sequence[]) {
// instruction* workers[2] = {nullptr, nullptr};
bool stop{false};
while (!stop) {
- for (size_t i = 0; i < ARRAY_SIZE(workers) && !stop; i++) {
- check(second, &workers[i]);
- }
+ check(second, workers, ARRAY_SIZE(workers));
stop = instruction::all_done();
second += int(!stop);
}