diff options
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 |