diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-05-22 17:54:14 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-05-22 17:54:14 +0800 |
commit | b2de16ed14b4d4d8ee9c02e5a509b124be116bf1 (patch) | |
tree | 092c6065d7a1368e00563ad64dee294d930627bb /src | |
parent | 0e98b4f87e2d1bf79320769a7bcf1d61a6c99532 (diff) | |
download | advent-of-code-b2de16ed14b4d4d8ee9c02e5a509b124be116bf1.tar.gz advent-of-code-b2de16ed14b4d4d8ee9c02e5a509b124be116bf1.zip |
2016 day9
Diffstat (limited to 'src')
-rw-r--r-- | src/2016/day9/README.md | 14 | ||||
-rw-r--r-- | src/2016/day9/aoc.cpp | 52 | ||||
-rw-r--r-- | src/2016/day9/aoc.h | 2 |
3 files changed, 53 insertions, 15 deletions
diff --git a/src/2016/day9/README.md b/src/2016/day9/README.md index 9d5ed19..b064cd2 100644 --- a/src/2016/day9/README.md +++ b/src/2016/day9/README.md @@ -15,3 +15,17 @@ A(2x2)BCD(2x2)EFG doubles the BC and EF, becoming ABCBCDEFEFG for a decompressed X(8x2)(3x3)ABCY becomes X(3x3)ABC(3x3)ABCY (for a decompressed length of 18), because the decompressed data from the (8x2) marker (the (3x3)ABC) is skipped and not processed further. What is the decompressed length of the file (your puzzle input)? Don't count whitespace. +--- Part Two --- +Apparently, the file actually uses version two of the format. + +In version two, the only difference is that markers within decompressed data are decompressed. This, the documentation explains, provides much more substantial compression capabilities, allowing many-gigabyte files to be stored in only a few kilobytes. + +For example: + +(3x3)XYZ still becomes XYZXYZXYZ, as the decompressed section contains no markers. +X(8x2)(3x3)ABCY becomes XABCABCABCABCABCABCY, because the decompressed data from the (8x2) marker is then further decompressed, thus triggering the (3x3) marker twice for a total of six ABC sequences. +(27x12)(20x12)(13x14)(7x10)(1x12)A decompresses into a string of A repeated 241920 times. +(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN becomes 445 characters long. +Unfortunately, the computer you brought probably doesn't have enough memory to actually decompress the file; you'll have to come up with another way to get its decompressed length. + +What is the decompressed length of the file using this improved format? diff --git a/src/2016/day9/aoc.cpp b/src/2016/day9/aoc.cpp index 4f741d5..83e72c6 100644 --- a/src/2016/day9/aoc.cpp +++ b/src/2016/day9/aoc.cpp @@ -12,7 +12,31 @@ static void get_number(const char** pp, int* d) { *pp = p; } -static void check(const char** pp, int* t) { +static void check0(int t, const char* p1, const char* p2, size_t* t1) { + size_t x{0}; + while (p1 < p2) { + if (*p1 == '(') { + int d[2] = {0}; + int i = 0; + while (*p1 != ')') { + if (*p1 >= '0' && *p1 <= '9') { + get_number(&p1, d + i++); + } + if (*p1 == '(' || *p1 == 'x') { + p1++; + } + } + check0(d[1], p1 + 1, p1 + d[0] + 1, &x); + p1 += d[0] + 1; + } else { + p1++; + x++; + } + } + *t1 += t * x; +} + +static void check(const char** pp, int* t0, size_t* t1) { int d[2] = {0}; const char* p = *pp; int i = 0; @@ -24,30 +48,30 @@ static void check(const char** pp, int* t) { p++; } } - p += d[0]; - *t += d[1] * d[0]; - *pp = p; + *t0 += d[1] * d[0]; + check0(d[1], p + 1, p + d[0] + 1, t1); + *pp = p + d[0] + 1; } -int day9(line_view file) { +std::pair<int, size_t> day9(line_view file) { int t0{0}; + size_t t1{0}; const char* p1 = file.line; - const char* p2 = p1 + file.length - 1; - const char* p = p1; + const char* p2 = p1 + file.length; - while (p < p2) { - if (*p == '(') { - check(&p, &t0); - p++; + while (p1 < p2) { + if (*p1 == '(') { + check(&p1, &t0, &t1); } else { - if (*p != ' ') { + if (*p1 != ' ' && *p1 != '\n') { t0++; + t1++; } - p++; + p1++; } } - return t0; + return {t0, t1}; } } // namespace aoc2016 diff --git a/src/2016/day9/aoc.h b/src/2016/day9/aoc.h index ab6a78c..f2401f0 100644 --- a/src/2016/day9/aoc.h +++ b/src/2016/day9/aoc.h @@ -3,5 +3,5 @@ namespace aoc2016 { -int day9(line_view); +std::pair<int, size_t> day9(line_view); } |