aboutsummaryrefslogtreecommitdiff
path: root/src/2018/day5/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-10 22:18:16 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-10 22:18:16 +0800
commitac146284fd54e11c7657a39f45fe8cc0213ebeb7 (patch)
treea19d1cf2ba1d2b18955b0723c8b647a6be921d0b /src/2018/day5/aoc.cpp
parent7518edc86fc1d77e09a624c97e71e2c7d877b54d (diff)
downloadadvent-of-code-ac146284fd54e11c7657a39f45fe8cc0213ebeb7.tar.gz
advent-of-code-ac146284fd54e11c7657a39f45fe8cc0213ebeb7.zip
2018 day5 part1
Diffstat (limited to 'src/2018/day5/aoc.cpp')
-rw-r--r--src/2018/day5/aoc.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/2018/day5/aoc.cpp b/src/2018/day5/aoc.cpp
index 585f144..5b6d3cb 100644
--- a/src/2018/day5/aoc.cpp
+++ b/src/2018/day5/aoc.cpp
@@ -1,5 +1,80 @@
#include "aoc.h"
+#include <math.h>
namespace aoc2018 {
+void deduct(char** pp1, char** pp2, char* b, char* e) {
+ char* p1 = *pp1;
+ char* p2 = *pp2;
+ while (p1 >= b && p2 < e) {
+ bool stop = true;
+ if (std::abs(*p1 - *p2) == 32) {
+ *p1 = ' ';
+ *p2 = ' ';
+ p1--;
+ p2++;
+ stop = false;
+ }
+ if (*p1 == ' ') {
+ p1--;
+ stop = false;
+ }
+ if (*p2 == ' ') {
+ p2++;
+ stop = false;
+ }
+ if (stop) {
+ break;
+ }
+ }
+ *pp1 = p1;
+ *pp2 = p2;
}
+
+int count(line_view lv) {
+ int total{0};
+ auto is_az = [](char c) { return c >= 'a' && c <= 'z'; };
+ auto is_AZ = [](char c) { return c >= 'A' && c <= 'Z'; };
+ per_char(lv, [&total, &is_az, &is_AZ](char c) {
+ if (is_az(c) || is_AZ(c)) {
+ total += 1;
+ }
+ return true;
+ });
+ return total;
+}
+
+void deduct(char* p1, char* p2) {
+ char* p = p1;
+ while (p < p2) {
+ if (std::abs(*p - *(p + 1)) == 32) {
+ char* pa = p;
+ char* pb = p + 1;
+ deduct(&pa, &pb, p1, p2);
+ p = pb;
+ } else {
+ p++;
+ }
+ }
+}
+
+void print(char* p1, char* p2) {
+ while (p1 < p2) {
+ if (*p1 != ' ') {
+ std::cout << *p1;
+ }
+ p1++;
+ }
+ printf("\n");
+}
+
+int day5(line_view file) {
+ char* p1 = (char*)malloc(file.length);
+ memcpy(p1, file.line, file.length);
+ char* p2 = p1 + file.length;
+ deduct(p1, p2);
+ // print(p1, p2);
+ return count({p1, p2});
+}
+
+} // namespace aoc2018