diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-20 13:39:11 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-20 13:39:11 +0800 |
commit | 787eb42f24cda88adfd0c041c08110581c0e11e1 (patch) | |
tree | a4eb28aa391608ebba909a1323f5e852925c462b /src/2018/day7/aoc.cpp | |
parent | ad940fa52f8bc45f07b5678eee4d94ee662e769b (diff) | |
download | advent-of-code-787eb42f24cda88adfd0c041c08110581c0e11e1.tar.gz advent-of-code-787eb42f24cda88adfd0c041c08110581c0e11e1.zip |
2018 day7
Diffstat (limited to 'src/2018/day7/aoc.cpp')
-rw-r--r-- | src/2018/day7/aoc.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/2018/day7/aoc.cpp b/src/2018/day7/aoc.cpp index 6210814..354bfc8 100644 --- a/src/2018/day7/aoc.cpp +++ b/src/2018/day7/aoc.cpp @@ -2,8 +2,9 @@ namespace aoc2018 { std::map<char, instruction*> instruction::instructions = {}; +size_t instruction::done_total = 0; -void day7(line_view file, char sequence[]) { +int day7(line_view file, char sequence[]) { per_line(file, [](line_view lv) { char p1 = *(lv.line + 5); char p2 = *(lv.line + 36); @@ -20,6 +21,36 @@ void day7(line_view file, char sequence[]) { n->make_done(); 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; + } + + if (*w == nullptr) { + instruction::next(w); + if (*w != nullptr) { + // std::cout << (*w)->label << " starts at " << s << std::endl; + (*w)->begin(s); + } + } + }; + + instruction::undo(); + int second{0}; + instruction* workers[5] = {nullptr, nullptr, nullptr, nullptr, nullptr}; + // 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]); + } + stop = instruction::all_done(); + second += int(!stop); + } + return second; } } // namespace aoc2018 |