aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-18 14:33:33 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-18 14:33:33 +0800
commit056c57d1808e9f11eda435729deda8b6d279ce91 (patch)
tree4b7dae6db7c191ecc85dfd277f7fe6c8c4bc012e
parent96f4ab93a21e6e0fc7213b2b24757110f8146d71 (diff)
downloadadvent-of-code-056c57d1808e9f11eda435729deda8b6d279ce91.tar.gz
advent-of-code-056c57d1808e9f11eda435729deda8b6d279ce91.zip
day8 done
-rw-r--r--src/2015/day8/README.md12
-rw-r--r--src/2015/day8/aoc.cpp30
-rw-r--r--src/2015/day8/aoc.h6
-rw-r--r--test/test_2015.cpp3
4 files changed, 36 insertions, 15 deletions
diff --git a/src/2015/day8/README.md b/src/2015/day8/README.md
index e9688bf..6783c87 100644
--- a/src/2015/day8/README.md
+++ b/src/2015/day8/README.md
@@ -19,4 +19,16 @@ Disregarding the whitespace in the file, what is the number of characters of cod
For example, given the four strings above, the total number of characters of string code (2 + 5 + 10 + 6 = 23) minus the total number of characters in memory for string values (0 + 3 + 7 + 1 = 11) is 23 - 11 = 12.
+--- Part Two ---
+
+Now, let's go the other way. In addition to finding the number of characters of code, you should now encode each code representation as a new string and find the number of characters of the new encoded representation, including the surrounding double quotes.
+
+For example:
+
+ "" encodes to "\"\"", an increase from 2 characters to 6.
+ "abc" encodes to "\"abc\"", an increase from 5 characters to 9.
+ "aaa\"aaa" encodes to "\"aaa\\\"aaa\"", an increase from 10 characters to 16.
+ "\x27" encodes to "\"\\x27\"", an increase from 6 characters to 11.
+
+Your task is to find the total number of characters to represent the newly encoded strings minus the number of characters of code in each original string literal. For example, for the strings above, the total encoded length (6 + 9 + 16 + 11 = 42) minus the characters in the original code representation (23, just like in the first part of this puzzle) is 42 - 23 = 19.
diff --git a/src/2015/day8/aoc.cpp b/src/2015/day8/aoc.cpp
index feae5dc..be789d2 100644
--- a/src/2015/day8/aoc.cpp
+++ b/src/2015/day8/aoc.cpp
@@ -5,43 +5,51 @@ namespace aoc2015 {
// \\ -> 1
// \" -> 1
// \xXX -> 1
-int count_day8(line_view lv) {
- int total = 0;
+std::pair<int, int> count_day8(line_view lv) {
+ int t1 = 0;
+ int t2 = 0;
const char* p1 = lv.line;
const char* p2 = lv.line + lv.length;
const char* p = p1;
while (p != p2) {
if (*p != '\\' || p == p2 - 1) {
- total += 1;
+ t1 += 1;
+ t2 += 1;
p++;
continue;
} else {
if (*(p + 1) == '\\' || *(p + 1) == '"') {
- total += 1;
+ t1 += 1;
+ t2 += 4;
p += 2;
continue;
}
if (*(p + 1) == 'x') {
- total += 1;
+ t1 += 1;
+ t2 += 5;
p += 4;
continue;
}
- total += 1;
+ t1 += 1;
+ t2 += 1;
p++;
}
}
- return total;
+ return {t1, t2};
}
-int day8(line_view file) {
+std::pair<int, int> day8(line_view file) {
int t1 = 0;
int t2 = 0;
- per_line(file, [&t1, &t2](line_view lv) {
+ int t3 = 0;
+ per_line(file, [&t1, &t2, &t3](line_view lv) {
t1 += lv.length - 1;
- t2 += count_day8({lv.line + 1, lv.line + lv.length - 2});
+ auto p = count_day8({lv.line + 1, lv.line + lv.length - 2});
+ t2 += p.first;
+ t3 += p.second + 6;
return true;
});
- return t1 - t2;
+ return {t1 - t2, t3 - t1};
}
} // namespace aoc2015
diff --git a/src/2015/day8/aoc.h b/src/2015/day8/aoc.h
index 5e9dafe..70a23b7 100644
--- a/src/2015/day8/aoc.h
+++ b/src/2015/day8/aoc.h
@@ -4,7 +4,7 @@
namespace aoc2015 {
-int count_day8(line_view);
-int day8(line_view);
+std::pair<int, int> count_day8(line_view);
+std::pair<int, int> day8(line_view);
-}
+} // namespace aoc2015
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index dfeb90d..291a894 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -114,5 +114,6 @@ TEST_CASE("Some Assembly Required", "[day7]") {
TEST_CASE("Matchsticks", "[day8]") {
line_view lv = load_file("../src/2015/day8/input");
auto p = aoc2015::day8(lv);
- REQUIRE(1371 == p);
+ REQUIRE(1371 == p.first);
+ REQUIRE(2117 == p.second);
}