diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-24 17:09:23 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-24 17:09:23 +0800 |
commit | 5c4a35c77539e969f4c20a0702402996b8848683 (patch) | |
tree | 7affb54ded18260434b657bcd4f02554e2281dd9 | |
parent | 8a3c5c59fb522334a2f181fc05cc148df5401cdb (diff) | |
download | advent-of-code-5c4a35c77539e969f4c20a0702402996b8848683.tar.gz advent-of-code-5c4a35c77539e969f4c20a0702402996b8848683.zip |
day19 part2 another too long
-rw-r--r-- | src/2015/day19/aoc.cpp | 3 | ||||
-rw-r--r-- | src/2015/day19/aoc.h | 37 | ||||
-rw-r--r-- | test/test_2015.cpp | 6 |
3 files changed, 36 insertions, 10 deletions
diff --git a/src/2015/day19/aoc.cpp b/src/2015/day19/aoc.cpp index 1fa17aa..0d69022 100644 --- a/src/2015/day19/aoc.cpp +++ b/src/2015/day19/aoc.cpp @@ -11,7 +11,8 @@ std::pair<int, int> day19(line_view file) { std::map<int, std::vector<molecule::change>> changes; m.check(changes); int shortest = INT32_MAX; - m.deduct(m.original, 0, &shortest); + // m.deduct(m.original, 0, &shortest); + m.transfer("e", 0, &shortest); return {m.distinct(changes), shortest}; } diff --git a/src/2015/day19/aoc.h b/src/2015/day19/aoc.h index 94f0659..a8e5b2d 100644 --- a/src/2015/day19/aoc.h +++ b/src/2015/day19/aoc.h @@ -1,6 +1,7 @@ #pragma once #include "common.h" +#include <algorithm> #include <map> #include <set> #include <string> @@ -93,10 +94,10 @@ struct molecule { // r.to -> r.from at p line_view replace(line_view lv, const replacement& r, const char* p) const noexcept { - size_t len = lv.length + r.from.length - r.to.length; + size_t len = lv.length + r.to.length - r.from.length; char* s = (char*)malloc(len); memset(s, 0x0, len); - const char* ps[] = {lv.line, p, p + r.to.length, lv.line + lv.length}; + const char* ps[] = {lv.line, p, p + r.from.length, lv.line + lv.length}; char* x = s; for (size_t i = 0; i < 3; i++) { if (i != 1) { @@ -106,8 +107,8 @@ struct molecule { memcpy(x, p1, p2 - p1); x += (p2 - p1); } else { - const char* p1 = r.from.line; - const char* p2 = r.from.line + r.from.length; + const char* p1 = r.to.line; + const char* p2 = r.to.line + r.to.length; // std::cout << line_view{p1, p2} << std::endl; memcpy(x, p1, p2 - p1); x += (p2 - p1); @@ -136,7 +137,7 @@ struct molecule { line_view v{p1, p2}; const char* p = v.contains(r.to); if (p != nullptr) { - line_view n = replace(lv, r, p); + line_view n = replace(lv, {r.to, r.from}, p); // step(steps); // std::cout << lv << " " << p - lv.line << " " << r.to << " -> " << n << std::endl; deduct(n, steps + 1, shortest); @@ -150,13 +151,36 @@ struct molecule { } } - void parse(line_view lv, std::vector<line_view>& ms) const noexcept {} + void parse(line_view lv, std::vector<line_view>& ms) const noexcept { + const char* p1 = lv.line; + const char* p2 = lv.line + lv.length; + while (p1 < p2) { + if (transfers.find({p1, 1}) != transfers.end()) { + ms.push_back({p1, 1}); + p1 += 1; + continue; + } + if (p1 < p2 - 1 && transfers.find({p1, 2}) != transfers.end()) { + ms.push_back({p1, 2}); + p1 += 2; + continue; + } + p1++; + } + } + + void sort() { + for (auto& kv : transfers) { + std::sort(kv.second.begin(), kv.second.end()); + } + } void transfer(line_view lv, int steps, int* shortest) { if (lv.length > original.length) { return; } if (lv == original) { + printf("e: %d\n", steps); if (*shortest > steps) { *shortest = steps; } @@ -167,6 +191,7 @@ struct molecule { for (auto& to : transfers[from]) { line_view next = replace(lv, {from, to}, from.line); transfer(next, steps + 1, shortest); + delete next.line; } } } diff --git a/test/test_2015.cpp b/test/test_2015.cpp index 442ffe1..8d22a72 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -201,9 +201,9 @@ TEST_CASE("Like a GIF For Your Yard", "[day18]") { } TEST_CASE("Medicine for Rudolph", "[day19]") { - // line_view lv = load_file("../src/2015/day19/input"); - line_view lv = line_view{"e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOHOHO\n"}; + line_view lv = load_file("../src/2015/day19/input"); + // line_view lv = line_view{"e => H\ne => O\nH => HO\nH => OH\nO => HH\n\nHOHOHO\n"}; auto p = aoc2015::day19(lv); // REQUIRE(509 == p.first); - REQUIRE(6 == p.second); + REQUIRE(0 == p.second); } |