diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-02-15 11:03:16 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-02-15 11:03:16 +0800 |
commit | b05722d7afc0972239308561c320af157790a0f9 (patch) | |
tree | 3c9bbd3005dfdd2d5ae743e89049dd30603af3f7 /src/2017/day21/aoc.cpp | |
parent | 0c9209192c955e7c4f0804704a87998b20d06673 (diff) | |
download | advent-of-code-b05722d7afc0972239308561c320af157790a0f9.tar.gz advent-of-code-b05722d7afc0972239308561c320af157790a0f9.zip |
2017 day21 part1
Diffstat (limited to 'src/2017/day21/aoc.cpp')
-rw-r--r-- | src/2017/day21/aoc.cpp | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/src/2017/day21/aoc.cpp b/src/2017/day21/aoc.cpp index c077b32..f3047e7 100644 --- a/src/2017/day21/aoc.cpp +++ b/src/2017/day21/aoc.cpp @@ -6,15 +6,67 @@ namespace aoc2017 { typedef void (*sfunc)(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map<std::string, std::string>& rs); -void s3tos2(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map<std::string, std::string>& rs) {} -void s2tos3(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map<std::string, std::string>& rs) {} +void s3tos2(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map<std::string, std::string>& rs) { + vs2->clear(); + for (auto& s : *vs3) { + std::string s4; + auto vs = s.combo(); + for (auto& c : vs) { + std::string x = c.to_string(); + auto it = rs.find(x); + if (it != rs.end()) { + s4 = it->second; + break; + } + } + if (s4.empty()) { + printf("no match %s\n", s.to_string().c_str()); + } + square4 x4{s4}; + for (auto&& s2 : x4.partition()) { + vs2->emplace_back(s2); + } + } + printf("s3[%zu] s2[%zu]\n", vs3->size(), vs2->size()); +} + +void s2tos3(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map<std::string, std::string>& rs) { + vs3->clear(); + for (auto& s : *vs2) { + std::string s3; + auto vs = s.combo(); + for (auto& c : vs) { + std::string x = c.to_string(); + auto it = rs.find(x); + if (it != rs.end()) { + s3 = it->second; + break; + } + } + if (s3.empty()) { + printf("no match %s\n", s.to_string().c_str()); + } + vs3->emplace_back(s3); + } + printf("s2[%zu] s3[%zu]\n", vs2->size(), vs3->size()); +} + +template <typename T> +static int count(std::vector<T>* v) { + int t{0}; + for (auto& s : *v) { + t += s.count(); + } + return t; +} static void part1(int t, int times, std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map<std::string, std::string>& rs) { sfunc fs[2] = {s3tos2, s2tos3}; - while (t <= times) { + while (t < times) { sfunc f = fs[t % 2]; f(vs3, vs2, rs); + printf("%d %d\n", count(vs3), count(vs2)); t++; } } @@ -46,7 +98,7 @@ std::pair<int64_t, int64_t> day21(line_view file) { // ### std::vector<square3> vs3{{".#./..#/###"}}; std::vector<square2> vs2; - part1(1, 5, &vs3, &vs2, rules); + part1(0, 6, &vs3, &vs2, rules); // for (auto& kv : rules) { // printf("%s => %s\n", kv.first.c_str(), kv.second.c_str()); |