aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-16 14:37:14 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-16 14:37:14 +0800
commitc69f8f28285fd507aa27ae0a18b614f371a2ae1d (patch)
tree9f3da71d7e0395d9a8e7124ae82ccde7e47e5cff /src
parent6aa919cbe332d7e45c0c3a7954d48166f4b774c0 (diff)
downloadadvent-of-code-c69f8f28285fd507aa27ae0a18b614f371a2ae1d.tar.gz
advent-of-code-c69f8f28285fd507aa27ae0a18b614f371a2ae1d.zip
day5 part1
Diffstat (limited to 'src')
-rw-r--r--src/2015/day4/README.md1
-rw-r--r--src/2015/day5/README.md18
-rw-r--r--src/2015/day5/aoc.cpp27
-rw-r--r--src/2015/day5/aoc.h2
-rw-r--r--src/common.h7
5 files changed, 41 insertions, 14 deletions
diff --git a/src/2015/day4/README.md b/src/2015/day4/README.md
index 99334b6..87ae375 100644
--- a/src/2015/day4/README.md
+++ b/src/2015/day4/README.md
@@ -10,6 +10,7 @@ For example:
If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
The first half of this puzzle is complete! It provides one gold star: *
+
--- Part Two ---
Now find one that starts with six zeroes.
diff --git a/src/2015/day5/README.md b/src/2015/day5/README.md
index dc50ed3..443ab73 100644
--- a/src/2015/day5/README.md
+++ b/src/2015/day5/README.md
@@ -18,3 +18,21 @@ For example:
How many strings are nice?
+--- Part Two ---
+
+Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
+
+Now, a nice string is one with all of the following properties:
+
+ It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
+ It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
+
+For example:
+
+ qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz).
+ xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap.
+ uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them.
+ ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
+
+How many strings are nice under these new rules?
+
diff --git a/src/2015/day5/aoc.cpp b/src/2015/day5/aoc.cpp
index d3f310b..aa0213f 100644
--- a/src/2015/day5/aoc.cpp
+++ b/src/2015/day5/aoc.cpp
@@ -15,7 +15,7 @@ int count_vowels(line_view lv, const char* vowels) {
bool is_nice(line_view lv, int repeated) {
const char* p = lv.line;
- while (p + repeated < lv.line + lv.length) {
+ while (p + repeated <= lv.line + lv.length) {
if (is_repeated(p, p + repeated)) {
return true;
} else {
@@ -25,16 +25,23 @@ bool is_nice(line_view lv, int repeated) {
return false;
}
-bool is_nice(line_view lv, const char* disallowed[]) {
- size_t size = sizeof(disallowed) / sizeof(*disallowed);
- if (std::any_of(disallowed, disallowed + size, [&lv](const char* s) -> bool { return lv.contains(s); })) {
- return false;
- }
- return true;
+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); });
}
-/*
-int day5(line_view) {}
-*/
+int day5(line_view file) {
+ int total = 0;
+ const char* subs[] = {"ab", "cd", "pq", "xy"};
+ per_line(file, [&subs, &total](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));
+ if (b1 && b2 && b3) {
+ total += 1;
+ }
+ return true;
+ });
+ return total;
+}
} // namespace aoc2015
diff --git a/src/2015/day5/aoc.h b/src/2015/day5/aoc.h
index beaa278..b338cc2 100644
--- a/src/2015/day5/aoc.h
+++ b/src/2015/day5/aoc.h
@@ -5,7 +5,7 @@ namespace aoc2015 {
int count_vowels(line_view, const char*);
bool is_nice(line_view, int);
-bool is_nice(line_view, const char*[]);
+bool is_nice(line_view, const char*[], size_t);
int day5(line_view);
diff --git a/src/common.h b/src/common.h
index 7f2dfc9..10c01e2 100644
--- a/src/common.h
+++ b/src/common.h
@@ -5,6 +5,8 @@
#include <string.h>
#include <utility>
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
+
struct line_view {
const char* line;
size_t length;
@@ -32,15 +34,14 @@ struct line_view {
bool contains(const char* s) {
size_t len = strlen(s);
const char* p = line;
- while (p + len < line + length) {
+ while (p + len <= line + length) {
if (*p == *s) {
line_view x{p, len};
if (x == s) {
return true;
- } else {
- p++;
}
}
+ p++;
}
return false;
}