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 | |
parent | 0c9209192c955e7c4f0804704a87998b20d06673 (diff) | |
download | advent-of-code-b05722d7afc0972239308561c320af157790a0f9.tar.gz advent-of-code-b05722d7afc0972239308561c320af157790a0f9.zip |
2017 day21 part1
-rw-r--r-- | src/2017/day21/aoc.cpp | 60 | ||||
-rw-r--r-- | src/2017/day21/aoc.h | 40 | ||||
-rw-r--r-- | src/2017/day21/input0 | 2 |
3 files changed, 66 insertions, 36 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()); diff --git a/src/2017/day21/aoc.h b/src/2017/day21/aoc.h index 1f743c2..4b2c464 100644 --- a/src/2017/day21/aoc.h +++ b/src/2017/day21/aoc.h @@ -20,18 +20,11 @@ struct square2 { std::vector<square2> flip() const noexcept { square2 s0; - s0.s[0] = s[1]; - s0.s[1] = s[0]; - s0.s[2] = s[3]; - s0.s[3] = s[2]; - - square2 s1; - s1.s[0] = s[2]; - s1.s[1] = s[3]; - s1.s[2] = s[0]; - s1.s[3] = s[1]; + for (int i = 0; i < 4; i++) { + s0.s[i] = s[i] == '#' ? '.' : '#'; + } - return {s0, s1}; + return {s0}; } std::vector<square2> rotate() const noexcept { @@ -106,28 +99,11 @@ struct square3 { std::vector<square3> flip() const noexcept { square3 s0; - s0.s[0] = s[2]; - s0.s[1] = s[1]; - s0.s[2] = s[0]; - s0.s[3] = s[5]; - s0.s[4] = s[4]; - s0.s[5] = s[3]; - s0.s[6] = s[8]; - s0.s[7] = s[7]; - s0.s[8] = s[6]; - - square3 s1; - s1.s[0] = s[6]; - s1.s[6] = s[0]; - s1.s[3] = s[3]; - s1.s[1] = s[7]; - s1.s[7] = s[1]; - s1.s[4] = s[4]; - s1.s[2] = s[8]; - s1.s[8] = s[2]; - s1.s[5] = s[5]; + for (int i = 0; i < 9; i++) { + s0.s[i] = s[i] == '#' ? '.' : '#'; + } - return {s0, s1}; + return {s0}; } std::vector<square3> rotate() const noexcept { diff --git a/src/2017/day21/input0 b/src/2017/day21/input0 index e69de29..f6d24df 100644 --- a/src/2017/day21/input0 +++ b/src/2017/day21/input0 @@ -0,0 +1,2 @@ +../.# => ##./#../... +.#./..#/### => #..#/..../..../#..# |