#include "aoc.h" #include namespace aoc2017 { struct rule { std::string k; std::string v; }; typedef void (*sfunc)(std::vector* vs3, std::vector* vs2, const std::vector& rs); template static std::string match(T t, const std::vector& 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) { // printf("%s matched %s\n", s.c_str(), r.k.c_str()); return r.v; } } } return ""; } void s3tos2(std::vector* vs3, std::vector* vs2, const std::vector& 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()); } 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* vs3, std::vector* vs2, const std::vector& rs) { vs3->clear(); for (auto& s : *vs2) { std::string s3 = match(s, rs); 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 static int count(std::vector* v) { int t{0}; for (auto& s : *v) { t += s.count(); } return t; } static void part1(int t, int times, std::vector* vs3, std::vector* vs2, const std::vector& rs) { sfunc fs[2] = {s3tos2, s2tos3}; while (t < times) { sfunc f = fs[t % 2]; f(vs3, vs2, rs); printf("%d %d\n", count(vs3), count(vs2)); t++; } } std::pair day21(line_view file) { std::vector rules; per_line(file, [&rules](line_view lv) { const char* p0 = lv.line; const char* p1 = p0; std::string k; std::string v; while (p1 < lv.line + lv.length) { if (*p1 == '=') { k = std::string{p0, (size_t)(p1 - p0 - 1)}; p0 = p1 + 3; } if (*p1 == '\n') { v = std::string{p0, (size_t)(p1 - p0)}; } p1++; } rules.emplace_back(rule{k, v}); return true; }); // .#. // ..# // ### std::vector vs3{{".#./..#/###"}}; std::vector vs2; part1(0, 5, &vs3, &vs2, rules); // for (auto& kv : rules) { // printf("%s => %s\n", kv.first.c_str(), kv.second.c_str()); // } return {0, 0}; } } // namespace aoc2017