aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day7/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-18 19:23:03 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-18 19:23:03 +0800
commit62889a8bdaa1fa0cd5f9a412d8171f212b4e1368 (patch)
treeafc850971c2a2db67319f2f967a99dd00920aa64 /src/2016/day7/aoc.cpp
parenta3487fedc77f01eb62725b9086942d63ffbb9f3c (diff)
downloadadvent-of-code-62889a8bdaa1fa0cd5f9a412d8171f212b4e1368.tar.gz
advent-of-code-62889a8bdaa1fa0cd5f9a412d8171f212b4e1368.zip
2016 day7
Diffstat (limited to 'src/2016/day7/aoc.cpp')
-rw-r--r--src/2016/day7/aoc.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/2016/day7/aoc.cpp b/src/2016/day7/aoc.cpp
index be28c50..b319a1a 100644
--- a/src/2016/day7/aoc.cpp
+++ b/src/2016/day7/aoc.cpp
@@ -1,4 +1,75 @@
#include "aoc.h"
+#include <vector>
namespace aoc2016 {
+
+bool is_abba(const char* p) { return *p == *(p + 3) && *(p + 1) == *(p + 2) && *p != *(p + 1); }
+bool is_aba(const char* p) { return *p == *(p + 2) && *p != *(p + 1); }
+bool is_reverse(const char* p1, const char* p2) { return *p1 == *(p2 + 1) && *(p1 + 1) == *p2; }
+
+std::vector<line_view> aba(const std::vector<line_view>& ls) {
+ std::vector<line_view> vs;
+ for (auto& l : ls) {
+ const char* p = l.line;
+ while (p < l.line + l.length - 2) {
+ if (is_aba(p)) {
+ vs.emplace_back(p, 3);
+ }
+ p++;
+ }
+ }
+ return vs;
+}
+
+bool match_aba(const std::vector<line_view>& ls1, const std::vector<line_view>& ls2) {
+ for (auto& l1 : ls1) {
+ for (auto& l2 : ls2) {
+ if (is_reverse(l1.line, l2.line)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+bool has_abba(const std::vector<line_view>& ls) {
+ for (auto& l : ls) {
+ const char* p = l.line;
+ while (p < l.line + l.length - 3) {
+ if (is_abba(p)) {
+ return true;
+ }
+ p++;
+ }
+ }
+ return false;
}
+
+std::pair<int, int> day7(line_view file) {
+ int t0{0}, t1{0};
+ per_line(file, [&t0, &t1](line_view lv) {
+ std::vector<line_view> g1;
+ std::vector<line_view> g2;
+ const char* p1 = lv.line;
+ const char* p = p1;
+ while (p < lv.line + lv.length) {
+ if (*p == '[') {
+ g1.emplace_back(p1, p);
+ p1 = p + 1;
+ }
+ if (*p == ']') {
+ g2.emplace_back(p1, p);
+ p1 = p + 1;
+ }
+ p++;
+ }
+ g1.emplace_back(p1, p);
+
+ t0 += int(has_abba(g1) && !has_abba(g2));
+ t1 += int(match_aba(aba(g1), aba(g2)));
+ return true;
+ });
+ return {t0, t1};
+}
+
+} // namespace aoc2016