aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/2018/day5/aoc.cpp37
-rw-r--r--src/2018/day5/aoc.h2
-rw-r--r--test/test_2018.cpp5
3 files changed, 39 insertions, 5 deletions
diff --git a/src/2018/day5/aoc.cpp b/src/2018/day5/aoc.cpp
index 5b6d3cb..4ce6e5c 100644
--- a/src/2018/day5/aoc.cpp
+++ b/src/2018/day5/aoc.cpp
@@ -52,12 +52,45 @@ void deduct(char* p1, char* p2) {
char* pb = p + 1;
deduct(&pa, &pb, p1, p2);
p = pb;
+ } else if (*p == ' ') {
+ char* pa = p - 1;
+ while (*p == ' ')
+ p++;
+ char* pb = p;
+ deduct(&pa, &pb, p1, p2);
+ p = pb;
} else {
p++;
}
}
}
+void remove(char* p1, char* p2, char c) {
+ while (p1 < p2) {
+ if (*p1 == c || *p1 == c - 32) {
+ *p1 = ' ';
+ }
+ p1++;
+ }
+}
+
+int shortest(line_view file) {
+ int s{INT32_MAX};
+ for (char i = 'a'; i <= 'z'; i++) {
+ char* p1 = (char*)malloc(file.length);
+ memcpy(p1, file.line, file.length);
+ char* p2 = p1 + file.length;
+ remove(p1, p2, i);
+ deduct(p1, p2);
+ int c = count({p1, p2});
+ if (c < s) {
+ s = c;
+ }
+ free(p1);
+ }
+ return s;
+}
+
void print(char* p1, char* p2) {
while (p1 < p2) {
if (*p1 != ' ') {
@@ -68,13 +101,13 @@ void print(char* p1, char* p2) {
printf("\n");
}
-int day5(line_view file) {
+std::pair<int, 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});
+ return {count({p1, p2}), shortest(file)};
}
} // namespace aoc2018
diff --git a/src/2018/day5/aoc.h b/src/2018/day5/aoc.h
index a6d2d5c..3c49886 100644
--- a/src/2018/day5/aoc.h
+++ b/src/2018/day5/aoc.h
@@ -4,5 +4,5 @@
namespace aoc2018 {
-int day5(line_view);
+std::pair<int, int> day5(line_view);
}
diff --git a/test/test_2018.cpp b/test/test_2018.cpp
index 1f183b1..64dcfb4 100644
--- a/test/test_2018.cpp
+++ b/test/test_2018.cpp
@@ -40,6 +40,7 @@ TEST_CASE("Repose Record", "[2018]") {
TEST_CASE("Alchemical Reduction", "[2018]") {
line_view lv = load_file("../src/2018/day5/input");
- REQUIRE(11042 == aoc2018::day5(lv));
- // REQUIRE(10 == aoc2018::day5("dabAcCaCBAcCcaDA"));
+ auto p = aoc2018::day5(lv);
+ REQUIRE(11042 == p.first);
+ REQUIRE(6872 == p.second);
}