aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day21/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2017/day21/aoc.cpp')
-rw-r--r--src/2017/day21/aoc.cpp61
1 files changed, 32 insertions, 29 deletions
diff --git a/src/2017/day21/aoc.cpp b/src/2017/day21/aoc.cpp
index f3047e7..876f1a0 100644
--- a/src/2017/day21/aoc.cpp
+++ b/src/2017/day21/aoc.cpp
@@ -3,22 +3,35 @@
namespace aoc2017 {
-typedef void (*sfunc)(std::vector<square3>* vs3, std::vector<square2>* vs2,
- const std::map<std::string, std::string>& rs);
+struct rule {
+ std::string k;
+ std::string v;
+};
-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;
+typedef void (*sfunc)(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::vector<rule>& rs);
+
+template <typename T>
+static std::string match(T t, const std::vector<rule>& rs) {
+ std::string s = t.to_string();
+ for (auto& r : rs) {
+ if (r.k == s) {
+ return r.v;
+ }
+ }
+ for (auto& r : rs) {
+ for (auto&& sx : t.combo()) {
+ if (sx.to_string() == r.k) {
+ return r.v;
}
}
+ }
+ return "";
+}
+
+void s3tos2(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::vector<rule>& rs) {
+ vs2->clear();
+ for (auto& s : *vs3) {
+ std::string s4 = match(s, rs);
if (s4.empty()) {
printf("no match %s\n", s.to_string().c_str());
}
@@ -30,19 +43,10 @@ void s3tos2(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::map
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) {
+void s2tos3(std::vector<square3>* vs3, std::vector<square2>* vs2, const std::vector<rule>& 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;
- }
- }
+ std::string s3 = match(s, rs);
if (s3.empty()) {
printf("no match %s\n", s.to_string().c_str());
}
@@ -60,8 +64,7 @@ static int count(std::vector<T>* v) {
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) {
+static void part1(int t, int times, std::vector<square3>* vs3, std::vector<square2>* vs2, const std::vector<rule>& rs) {
sfunc fs[2] = {s3tos2, s2tos3};
while (t < times) {
sfunc f = fs[t % 2];
@@ -72,7 +75,7 @@ static void part1(int t, int times, std::vector<square3>* vs3, std::vector<squar
}
std::pair<int64_t, int64_t> day21(line_view file) {
- std::map<std::string, std::string> rules;
+ std::vector<rule> rules;
per_line(file, [&rules](line_view lv) {
const char* p0 = lv.line;
@@ -89,7 +92,7 @@ std::pair<int64_t, int64_t> day21(line_view file) {
}
p1++;
}
- rules.insert({k, v});
+ rules.emplace_back(rule{k, v});
return true;
});
@@ -98,7 +101,7 @@ std::pair<int64_t, int64_t> day21(line_view file) {
// ###
std::vector<square3> vs3{{".#./..#/###"}};
std::vector<square2> vs2;
- part1(0, 6, &vs3, &vs2, rules);
+ part1(0, 5, &vs3, &vs2, rules);
// for (auto& kv : rules) {
// printf("%s => %s\n", kv.first.c_str(), kv.second.c_str());