diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-22 13:58:50 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-22 13:58:50 +0800 |
commit | b03d29360fa8acd59c613345ec35bd0e6686cbc4 (patch) | |
tree | f5e4c5cf4f5ae145e083a561bee3277074bfbb92 | |
parent | 922a44312b3d896dd1ce9c9790f11a6afd855dc9 (diff) | |
download | advent-of-code-b03d29360fa8acd59c613345ec35bd0e6686cbc4.tar.gz advent-of-code-b03d29360fa8acd59c613345ec35bd0e6686cbc4.zip |
day19 part1
-rw-r--r-- | src/2015/day19/aoc.h | 37 | ||||
-rw-r--r-- | test/test_2015.cpp | 6 |
2 files changed, 38 insertions, 5 deletions
diff --git a/src/2015/day19/aoc.h b/src/2015/day19/aoc.h index b357ab1..a89aa09 100644 --- a/src/2015/day19/aoc.h +++ b/src/2015/day19/aoc.h @@ -2,6 +2,8 @@ #include "common.h" #include <map> +#include <set> +#include <string> #include <vector> namespace aoc2015 { @@ -42,11 +44,42 @@ struct molecule { } } + std::string apply(const change& c) const noexcept { + int len = original.length + c.c.to.length - c.c.from.length + 1; + // int len = original.length + 1; + char* s = (char*)malloc(len); + memset(s, 0x0, len); + // const char* ps[] = {original.line, c.c.from.line, c.c.from.line + c.c.from.length, original.line + + // original.length}; + const char* ps[] = {original.line, c.c.from.line, c.c.from.line + c.c.from.length, original.line + original.length}; + char* p = s; + for (size_t i = 0; i < 3; i++) { + if (i != 1) { + const char* p1 = ps[i]; + const char* p2 = ps[i + 1]; + // std::cout << line_view{p1, p2} << std::endl; + memcpy(p, p1, p2 - p1); + p += (p2 - p1); + } else { + const char* p1 = c.c.to.line; + const char* p2 = c.c.to.line + c.c.to.length; + // std::cout << line_view{p1, p2} << std::endl; + memcpy(p, p1, p2 - p1); + p += (p2 - p1); + } + } + return std::string(s); + } + int distinct(const std::vector<change>& vc) const noexcept { + std::set<std::string> ss; for (auto& c : vc) { - std::cout << c.i << ": " << original << " " << (c.c.from.line - original.line) << " " << c.c.to << std::endl; + // std::cout << c.i << ": " << original << " " << (c.c.from.line - original.line) << " " << c.c.to << std::endl; + auto s = apply(c); + // std::cout << "<" << s.length() << ":" << s << ">" << std::endl; + ss.insert(s); } - return 0; + return ss.size(); } int distinct(const std::map<int, std::vector<change>>& vr) const noexcept { diff --git a/test/test_2015.cpp b/test/test_2015.cpp index ef76a14..ee27493 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -201,7 +201,7 @@ 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{"H => HO\nH => OH\nO => HH\n\nHOH\n"}; - aoc2015::day19(lv); + line_view lv = load_file("../src/2015/day19/input"); + // line_view lv = line_view{"H => HO\nH => OH\nO => HH\n\nHOH\n"}; + REQUIRE(0 == aoc2015::day19(lv)); } |