diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-25 00:13:17 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-25 00:13:17 +0800 |
commit | 44f63ae43ba08394cc0ff374ace5a707aa3c08bf (patch) | |
tree | cab41fb62357167ecc97b817f66eea5919011ca0 /src | |
parent | 94c77ee143b8ba663d86fff3be7d42f25ee96a43 (diff) | |
download | advent-of-code-44f63ae43ba08394cc0ff374ace5a707aa3c08bf.tar.gz advent-of-code-44f63ae43ba08394cc0ff374ace5a707aa3c08bf.zip |
parse pattern
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day19/aoc.h | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/2015/day19/aoc.h b/src/2015/day19/aoc.h index 5c19adf..62b4785 100644 --- a/src/2015/day19/aoc.h +++ b/src/2015/day19/aoc.h @@ -24,6 +24,32 @@ struct molecule { replacement c; }; + //...Rn.Y.Y.Ar + const char* parse_pattern(line_view lv, std::vector<line_view>& ps) { + const char* p1 = lv.line; + const char* p2 = lv.line + lv.length; + const char* p = p1; + while (p < p2) { + if (*p == 'R') { + const char* a = parse_pattern({p+2, p2}, ps); + p = a + 2; + continue; + } + if (*p == 'A' && *(p + 1) == 'r') { + ps.push_back({p1, p}); + return p; + } + if (*p == 'Y') { + ps.push_back({p1, p}); + p++; + p1 = p; + continue; + } + p++; + } + return nullptr; + } + void check(size_t i, replacement r, std::map<int, std::vector<change>>& vr) const noexcept { const char* p1 = original.line; const char* p2 = original.line + original.length; @@ -123,7 +149,7 @@ struct molecule { } } - // exponetial down + // exponential down void deduct(line_view lv, int steps, int* shortest) const noexcept { if (lv == "e") { if (*shortest > steps) { @@ -176,7 +202,7 @@ struct molecule { } } - // exponetial up + // exponential up void transfer(line_view lv, int steps, int* shortest) { if (lv.length > original.length) { return; |