aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-16 17:13:23 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-16 17:13:23 +0800
commit6d1b0e8ca4553c22c39fddbaf5ae97e5d5fe8add (patch)
treed63d6a89527b129b8615edf012bbbc2a9637581b
parent48da21bd1e54f1efd0bbca23b67cced4a94d1447 (diff)
downloadadvent-of-code-6d1b0e8ca4553c22c39fddbaf5ae97e5d5fe8add.tar.gz
advent-of-code-6d1b0e8ca4553c22c39fddbaf5ae97e5d5fe8add.zip
day 5
-rw-r--r--src/2015/day5/aoc.cpp31
-rw-r--r--src/2015/day5/aoc.h3
-rw-r--r--src/common.cpp4
-rw-r--r--src/common.h7
-rw-r--r--test/test_2015.cpp9
5 files changed, 40 insertions, 14 deletions
diff --git a/src/2015/day5/aoc.cpp b/src/2015/day5/aoc.cpp
index 6c5ca47..86d142f 100644
--- a/src/2015/day5/aoc.cpp
+++ b/src/2015/day5/aoc.cpp
@@ -37,23 +37,44 @@ bool is_interleaved(line_view lv) {
return false;
}
+bool has_no_overlap_pair(line_view lv) {
+ const char* p1 = lv.line;
+ const char* p2 = lv.line + lv.length;
+ while (p1 + 2 <= p2) {
+ line_view h{p1, 2};
+ line_view t{p1 + 2, p2};
+ if (t.contains(h) != nullptr) {
+ return true;
+ } else {
+ p1++;
+ }
+ }
+ return false;
+}
+
bool is_nice(line_view lv, const char* disallowed[], size_t size) {
return std::all_of(disallowed, disallowed + size, [&lv](const char* s) -> bool { return lv.contains(s) == nullptr; });
}
-int day5(line_view file) {
- int total = 0;
+std::pair<int,int> day5(line_view file) {
+ int total0 = 0;
+ int total1 = 0;
const char* subs[] = {"ab", "cd", "pq", "xy"};
- per_line(file, [&subs, &total](line_view lv) {
+ per_line(file, [&subs, &total0, &total1](line_view lv) {
auto b1 = count_vowels(lv, "aeiou") >= 3;
auto b2 = is_nice(lv, 2);
auto b3 = is_nice(lv, subs, ARRAY_SIZE(subs));
+ auto b4 = is_interleaved(lv);
+ auto b5 = has_no_overlap_pair(lv);
if (b1 && b2 && b3) {
- total += 1;
+ total0 += 1;
+ }
+ if (b4 && b5) {
+ total1 += 1;
}
return true;
});
- return total;
+ return {total0, total1};
}
} // namespace aoc2015
diff --git a/src/2015/day5/aoc.h b/src/2015/day5/aoc.h
index a75f3cb..7cc0a1b 100644
--- a/src/2015/day5/aoc.h
+++ b/src/2015/day5/aoc.h
@@ -7,7 +7,8 @@ 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);
+bool has_no_overlap_pair(line_view);
-int day5(line_view);
+std::pair<int,int> day5(line_view);
}
diff --git a/src/common.cpp b/src/common.cpp
index 1342109..c768f77 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -10,9 +10,9 @@ line_view load_file(const char* path) {
struct stat fs;
fd = open(path, O_RDONLY);
if (fd == -1)
- return {nullptr, 0};
+ return {nullptr, size_t(0)};
if (fstat(fd, &fs) == -1)
- return {nullptr, 0};
+ return {nullptr, size_t(0)};
line_view lv;
lv.length = fs.st_size;
diff --git a/src/common.h b/src/common.h
index a932f3b..8d358cb 100644
--- a/src/common.h
+++ b/src/common.h
@@ -24,15 +24,14 @@ struct line_view {
bool operator==(const line_view& lv) const noexcept {
const char* p1 = line;
const char* p2 = lv.line;
+ const char* p3 = p1 + length;
if (length != lv.length) {
return false;
}
- while (p1 < p1 + length) {
- if (*p1 != *p2) {
+ while (p1 < p3) {
+ if (*p1++ != *p2++) {
return false;
}
- p1++;
- p2++;
}
return true;
}
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index 03ab285..b9582c6 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -59,10 +59,15 @@ TEST_CASE("Doesn't He Have Intern-Elves For This?", "[day5]") {
const char* sub1[] = {"xy", "dx"};
const char* sub2[] = {"xy", "fadd", "dx"};
- REQUIRE(lv.contains(sub2[1]));
+ REQUIRE(lv.contains("fadd"));
+ REQUIRE(lv.contains("ad"));
REQUIRE(aoc2015::is_nice(lv, sub1, ARRAY_SIZE(sub1)));
REQUIRE(!aoc2015::is_nice(lv, sub2, ARRAY_SIZE(sub2)));
+ REQUIRE(aoc2015::is_interleaved(lv));
+ REQUIRE(aoc2015::has_no_overlap_pair(lv));
line_view ss = load_file("../src/2015/day5/input");
- REQUIRE(aoc2015::day5(ss) == 255);
+ auto p = aoc2015::day5(ss);
+ REQUIRE(p.first == 255);
+ REQUIRE(p.second == 55);
}