aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day19/aoc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/2015/day19/aoc.h')
-rw-r--r--src/2015/day19/aoc.h37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/2015/day19/aoc.h b/src/2015/day19/aoc.h
index b357ab1..a89aa09 100644
--- a/src/2015/day19/aoc.h
+++ b/src/2015/day19/aoc.h
@@ -2,6 +2,8 @@
#include "common.h"
#include <map>
+#include <set>
+#include <string>
#include <vector>
namespace aoc2015 {
@@ -42,11 +44,42 @@ struct molecule {
}
}
+ std::string apply(const change& c) const noexcept {
+ int len = original.length + c.c.to.length - c.c.from.length + 1;
+ // int len = original.length + 1;
+ char* s = (char*)malloc(len);
+ memset(s, 0x0, len);
+ // const char* ps[] = {original.line, c.c.from.line, c.c.from.line + c.c.from.length, original.line +
+ // original.length};
+ const char* ps[] = {original.line, c.c.from.line, c.c.from.line + c.c.from.length, original.line + original.length};
+ char* p = s;
+ for (size_t i = 0; i < 3; i++) {
+ if (i != 1) {
+ const char* p1 = ps[i];
+ const char* p2 = ps[i + 1];
+ // std::cout << line_view{p1, p2} << std::endl;
+ memcpy(p, p1, p2 - p1);
+ p += (p2 - p1);
+ } else {
+ const char* p1 = c.c.to.line;
+ const char* p2 = c.c.to.line + c.c.to.length;
+ // std::cout << line_view{p1, p2} << std::endl;
+ memcpy(p, p1, p2 - p1);
+ p += (p2 - p1);
+ }
+ }
+ return std::string(s);
+ }
+
int distinct(const std::vector<change>& vc) const noexcept {
+ std::set<std::string> ss;
for (auto& c : vc) {
- std::cout << c.i << ": " << original << " " << (c.c.from.line - original.line) << " " << c.c.to << std::endl;
+ // std::cout << c.i << ": " << original << " " << (c.c.from.line - original.line) << " " << c.c.to << std::endl;
+ auto s = apply(c);
+ // std::cout << "<" << s.length() << ":" << s << ">" << std::endl;
+ ss.insert(s);
}
- return 0;
+ return ss.size();
}
int distinct(const std::map<int, std::vector<change>>& vr) const noexcept {