aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day11
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-18 23:18:12 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-18 23:18:12 +0800
commit0207a5a0e5f5ba522f0b005eee3f269032b01c6d (patch)
treeff21a70507b6456807fb8d52090b2b95527db1df /src/2015/day11
parent057a878b90cc72930a5e4fa312845fec00270a49 (diff)
downloadadvent-of-code-0207a5a0e5f5ba522f0b005eee3f269032b01c6d.tar.gz
advent-of-code-0207a5a0e5f5ba522f0b005eee3f269032b01c6d.zip
day11 done
Diffstat (limited to 'src/2015/day11')
-rw-r--r--src/2015/day11/README.md27
-rw-r--r--src/2015/day11/aoc.cpp91
-rw-r--r--src/2015/day11/aoc.h6
3 files changed, 124 insertions, 0 deletions
diff --git a/src/2015/day11/README.md b/src/2015/day11/README.md
new file mode 100644
index 0000000..989ab88
--- /dev/null
+++ b/src/2015/day11/README.md
@@ -0,0 +1,27 @@
+--- Day 11: Corporate Policy ---
+
+Santa's previous password expired, and he needs help choosing a new one.
+
+To help him remember his new password after the old one expires, Santa has devised a method of coming up with a password based on the previous one. Corporate policy dictates that passwords must be exactly eight lowercase letters (for security reasons), so he finds his new password by incrementing his old password string repeatedly until it is valid.
+
+Incrementing is just like counting with numbers: xx, xy, xz, ya, yb, and so on. Increase the rightmost letter one step; if it was z, it wraps around to a, and repeat with the next letter to the left until one doesn't wrap around.
+
+Unfortunately for Santa, a new Security-Elf recently started, and he has imposed some additional password requirements:
+
+ Passwords must include one increasing straight of at least three letters, like abc, bcd, cde, and so on, up to xyz. They cannot skip letters; abd doesn't count.
+ Passwords may not contain the letters i, o, or l, as these letters can be mistaken for other characters and are therefore confusing.
+ Passwords must contain at least two different, non-overlapping pairs of letters, like aa, bb, or zz.
+
+For example:
+
+ hijklmmn meets the first requirement (because it contains the straight hij) but fails the second requirement requirement (because it contains i and l).
+ abbceffg meets the third requirement (because it repeats bb and ff) but fails the first requirement.
+ abbcegjk fails the third requirement, because it only has one double letter (bb).
+ The next password after abcdefgh is abcdffaa.
+ The next password after ghijklmn is ghjaabcc, because you eventually skip all the passwords that start with ghi..., since i is not allowed.
+
+Given Santa's current password (your puzzle input), what should his next password be?
+
+Your puzzle input is hxbxwxba.
+
+
diff --git a/src/2015/day11/aoc.cpp b/src/2015/day11/aoc.cpp
new file mode 100644
index 0000000..3489c53
--- /dev/null
+++ b/src/2015/day11/aoc.cpp
@@ -0,0 +1,91 @@
+#include "aoc.h"
+
+namespace aoc2015 {
+
+bool is_valid_rule1(int* is) {
+ auto is_valid = [](int* p) -> bool { return (*p + 1 == *(p + 1)) && (*p + 2 == *(p + 2)); };
+ for (int i = 0; i < 6; i++) {
+ if (is_valid(is + i)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+bool is_valid_rule3(int* is) {
+ int* p1 = nullptr;
+ int* p2 = nullptr;
+ int* p3 = is + 7;
+ while (is < p3) {
+ if (*is == *(is + 1)) {
+ if (p1 == nullptr) {
+ p1 = is;
+ } else {
+ p2 = is;
+ }
+ is += 2;
+ } else {
+ is += 1;
+ }
+ }
+ return p1 != nullptr && p2 != nullptr && *p1 != *p2;
+}
+
+// int* 0..25
+void next(int* is, int* invalids, int n) {
+ // increatment
+ for (int x = 7; x >= 0; x--) {
+ if (is[x] == 'z' - 'a') {
+ is[x] = 0;
+ } else {
+ is[x] += 1;
+ break;
+ }
+ }
+
+ auto is_invalid = [invalids, n](int x) -> bool {
+ for (int j = 0; j < n; j++) {
+ if (x == invalids[j]) {
+ return true;
+ }
+ }
+ return false;
+ };
+
+ for (int x = 0; x < 8; x++) {
+ if (is_invalid(is[x])) { // is[x] = 'i' 'j' 'o'
+ is[x] = is[x] == 25 ? 0 : is[x] + 1;
+ for (int k = x + 1; k < 8; k++) {
+ is[k] = 0;
+ }
+ break;
+ }
+ }
+}
+
+char* day11(const char* pass) {
+ int invalids[] = {'i' - 'a', 'o' - 'a', 'l' - 'a'};
+ int indexes[8] = {0};
+ for (int i = 0; i < 8; i++) {
+ indexes[i] = pass[i] - 'a';
+ }
+
+ bool b1 = false;
+ bool b3 = false;
+
+ do {
+ next(indexes, invalids, ARRAY_SIZE(invalids));
+ b1 = is_valid_rule1(indexes);
+ b3 = is_valid_rule3(indexes);
+ } while (!b1 || !b3);
+
+ char* password = (char*)malloc(9);
+ for (int i = 0; i < 8; i++) {
+ password[i] = 'a' + indexes[i];
+ }
+ password[8] = '\0';
+
+ return password;
+}
+
+} // namespace aoc2015
diff --git a/src/2015/day11/aoc.h b/src/2015/day11/aoc.h
new file mode 100644
index 0000000..a0706d9
--- /dev/null
+++ b/src/2015/day11/aoc.h
@@ -0,0 +1,6 @@
+#pragma once
+#include "common.h"
+
+namespace aoc2015 {
+char * day11(const char *);
+}