1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#include "aoc.h"
#include <map>
namespace aoc2017 {
struct rule {
std::string k;
std::string v;
};
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());
}
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::vector<rule>& 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 <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::vector<rule>& 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<int64_t, int64_t> day21(line_view file) {
std::vector<rule> 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<square3> vs3{{".#./..#/###"}};
std::vector<square2> 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
|