diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-22 15:46:55 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-22 15:46:55 +0800 |
commit | 6c4f0e193314b179f9ffb81b00c1b36c69e3e430 (patch) | |
tree | 9bc325e2fbe2ac0bce42b4e96704074a485fe898 | |
parent | 79c4ecc0eaca2119486821c3a70daaf69601c1b3 (diff) | |
download | advent-of-code-6c4f0e193314b179f9ffb81b00c1b36c69e3e430.tar.gz advent-of-code-6c4f0e193314b179f9ffb81b00c1b36c69e3e430.zip |
day19 part2
-rw-r--r-- | src/2015/day19/aoc.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/2015/day19/aoc.h b/src/2015/day19/aoc.h index a89aa09..39fa7a5 100644 --- a/src/2015/day19/aoc.h +++ b/src/2015/day19/aoc.h @@ -90,6 +90,32 @@ struct molecule { return d; } + line_view replace(line_view lv, const replacement& r, const char* p) const noexcept { return {}; } + + void deduct(line_view lv, int steps, int* shortest) const noexcept { + if (lv == "e") { + if (*shortest > steps) { + *shortest = steps; + } + } else { + const char* p1 = lv.line; + const char* p2 = lv.line + lv.length; + for (auto& r : replacements) { + do { + line_view v{p1, p2}; + const char* p = v.contains(r.to); + if (p != nullptr) { + line_view n = replace(v, r, p); + deduct(n, steps + 1, shortest); + p1 = p + r.to.length; + } else { + break; + } + } while (p1 < p2); + } + } + } + void parse(line_view lv) { const char* p = lv.contains("=>"); if (p != nullptr) { |