diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-23 22:45:40 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-23 22:45:40 +0800 |
commit | 8a3c5c59fb522334a2f181fc05cc148df5401cdb (patch) | |
tree | b64e61b505cc64779da57ca75717786c91790901 /src | |
parent | d080e1ade488eb01327d5d0f8a0f0bd9db469b2d (diff) | |
download | advent-of-code-8a3c5c59fb522334a2f181fc05cc148df5401cdb.tar.gz advent-of-code-8a3c5c59fb522334a2f181fc05cc148df5401cdb.zip |
day19 another way
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day19/aoc.h | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/2015/day19/aoc.h b/src/2015/day19/aoc.h index e7f592a..94f0659 100644 --- a/src/2015/day19/aoc.h +++ b/src/2015/day19/aoc.h @@ -15,6 +15,7 @@ struct replacement { struct molecule { std::vector<replacement> replacements; + std::map<line_view, std::vector<line_view>> transfers; line_view original; struct change { @@ -127,8 +128,7 @@ struct molecule { *shortest = steps; } return; - } - else { + } else { for (auto& r : replacements) { const char* p1 = lv.line; const char* p2 = lv.line + lv.length; @@ -150,10 +150,33 @@ struct molecule { } } + void parse(line_view lv, std::vector<line_view>& ms) const noexcept {} + + void transfer(line_view lv, int steps, int* shortest) { + if (lv.length > original.length) { + return; + } + if (lv == original) { + if (*shortest > steps) { + *shortest = steps; + } + } else { + std::vector<line_view> ms; + parse(lv, ms); + for (auto& from : ms) { + for (auto& to : transfers[from]) { + line_view next = replace(lv, {from, to}, from.line); + transfer(next, steps + 1, shortest); + } + } + } + } + void parse(line_view lv) { const char* p = lv.contains("=>"); if (p != nullptr) { replacements.push_back({{lv.line, p - 1}, {p + 3, lv.line + lv.length - 1}}); + transfers[{lv.line, p - 1}].push_back({p + 3, lv.line + lv.length - 1}); } else { if (lv.length > 1) { original = {lv.line, lv.line + lv.length - 1}; |