aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-16 15:50:16 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-16 15:50:16 +0800
commit48da21bd1e54f1efd0bbca23b67cced4a94d1447 (patch)
treef24146ea4192dfc4fc14d586e89e4428a1662f0b
parentc69f8f28285fd507aa27ae0a18b614f371a2ae1d (diff)
downloadadvent-of-code-48da21bd1e54f1efd0bbca23b67cced4a94d1447.tar.gz
advent-of-code-48da21bd1e54f1efd0bbca23b67cced4a94d1447.zip
line view
-rw-r--r--src/2015/day5/aoc.cpp14
-rw-r--r--src/2015/day5/aoc.h1
-rw-r--r--src/common.h41
3 files changed, 52 insertions, 4 deletions
diff --git a/src/2015/day5/aoc.cpp b/src/2015/day5/aoc.cpp
index aa0213f..6c5ca47 100644
--- a/src/2015/day5/aoc.cpp
+++ b/src/2015/day5/aoc.cpp
@@ -25,8 +25,20 @@ bool is_nice(line_view lv, int repeated) {
return false;
}
+bool is_interleaved(line_view lv) {
+ const char* p = lv.line;
+ while (p + 3 <= lv.line + lv.length) {
+ if (*p == *(p + 2)) {
+ return true;
+ } else {
+ p++;
+ }
+ }
+ return false;
+}
+
bool is_nice(line_view lv, const char* disallowed[], size_t size) {
- return not std::any_of(disallowed, disallowed + size, [&lv](const char* s) -> bool { return lv.contains(s); });
+ return std::all_of(disallowed, disallowed + size, [&lv](const char* s) -> bool { return lv.contains(s) == nullptr; });
}
int day5(line_view file) {
diff --git a/src/2015/day5/aoc.h b/src/2015/day5/aoc.h
index b338cc2..a75f3cb 100644
--- a/src/2015/day5/aoc.h
+++ b/src/2015/day5/aoc.h
@@ -6,6 +6,7 @@ namespace aoc2015 {
int count_vowels(line_view, const char*);
bool is_nice(line_view, int);
bool is_nice(line_view, const char*[], size_t);
+bool is_interleaved(line_view);
int day5(line_view);
diff --git a/src/common.h b/src/common.h
index 10c01e2..a932f3b 100644
--- a/src/common.h
+++ b/src/common.h
@@ -13,6 +13,7 @@ struct line_view {
line_view(const char* s = nullptr) : line(s) { length = s == nullptr ? 0 : strlen(s); }
line_view(const char* s, size_t l) : line(s), length(l) {}
+ line_view(const char* p1, const char* p2) : line(p1), length(p2 - p1 + 1) {}
friend std::ostream& operator<<(std::ostream& o, const line_view& lv) {
for (size_t i = 0; i < lv.length; i++) {
o << lv.line[i];
@@ -20,6 +21,40 @@ struct line_view {
return o;
}
+ bool operator==(const line_view& lv) const noexcept {
+ const char* p1 = line;
+ const char* p2 = lv.line;
+ if (length != lv.length) {
+ return false;
+ }
+ while (p1 < p1 + length) {
+ if (*p1 != *p2) {
+ return false;
+ }
+ p1++;
+ p2++;
+ }
+ return true;
+ }
+
+ const char* contains(const line_view& lv) const noexcept {
+ const char* p1 = line;
+ const char* p2 = line + length;
+ if (length < lv.length) {
+ return nullptr;
+ }
+ while (p1 + lv.length <= p2) {
+ if (*p1 == *lv.line) {
+ line_view x{p1, lv.length};
+ if (x == lv) {
+ return p1;
+ }
+ }
+ p1++;
+ }
+ return nullptr;
+ }
+
bool operator==(const char* l) const noexcept {
size_t i = 0;
while (i < this->length) {
@@ -31,19 +66,19 @@ struct line_view {
return l[i] == '\0';
}
- bool contains(const char* s) {
+ const char* contains(const char* s) const noexcept {
size_t len = strlen(s);
const char* p = line;
while (p + len <= line + length) {
if (*p == *s) {
line_view x{p, len};
if (x == s) {
- return true;
+ return p;
}
}
p++;
}
- return false;
+ return nullptr;
}
};