diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-26 18:59:28 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-26 18:59:28 +0800 |
commit | b24fbdd568fb1bc8a69aa4d7fe2d7a0812c9af86 (patch) | |
tree | 7078ce30f0ab80beb0a02777eb560b3b5983af35 | |
parent | e9945d14975c9fe57133c67834ab65b3c48021a8 (diff) | |
download | advent-of-code-b24fbdd568fb1bc8a69aa4d7fe2d7a0812c9af86.tar.gz advent-of-code-b24fbdd568fb1bc8a69aa4d7fe2d7a0812c9af86.zip |
2016 day21 part2
-rw-r--r-- | src/2016/day21/aoc.cpp | 122 | ||||
-rw-r--r-- | src/2016/day21/aoc.h | 2 | ||||
-rw-r--r-- | test/test_2016.cpp | 8 |
3 files changed, 88 insertions, 44 deletions
diff --git a/src/2016/day21/aoc.cpp b/src/2016/day21/aoc.cpp index 8c72582..fc83bb4 100644 --- a/src/2016/day21/aoc.cpp +++ b/src/2016/day21/aoc.cpp @@ -1,4 +1,5 @@ #include "aoc.h" +#include <stack> namespace aoc2016 { @@ -41,14 +42,32 @@ static void rotate(char cs[8], int left, int right) { } } +static void undo_rotate(char cs[8], int left, int right) { rotate(cs, right, left); } + // rotate based on position of letter X // determine the index of letter X // rotate the string to the right one time // plus a number of times equal to that index // plus one additional time if the index was at least 4 -static void rotate_char(char cs[8], int x, int) { +static void rotate_char(char cs[8], int x, int y) { int i = index(cs, x); - rotate(cs, 0, i >= 4 ? 2 + i : 1 + i); + int s = i >= 4 ? 2 + i : 1 + i; + rotate(cs, 0, s); +} + +static void undo_rotate_char(char cs[8], int x, int y) { + char xs[8] = {0}; + int left = 8; + for (int i = 0; i < 8; i++) { + memcpy(xs, cs, 8); + rotate(xs, i, 0); + rotate_char(xs, x, y); + if (memcmp(xs, cs, 8) == 0) { + left = i; + break; + } + } + rotate(cs, left, 0); } // reverse positions X through Y @@ -77,6 +96,8 @@ static void move(char cs[8], int x, int y) { cs[y] = c; } +static void undo_move(char cs[8], int x, int y) { move(cs, y, x); } + typedef void (*parse)(const char*, int ds[2]); static void parse_swap(const char* p, int ds[2]) { ds[0] = *(p + 14) - '0'; @@ -112,59 +133,80 @@ static void parse_move(const char* p, int ds[2]) { ds[1] = *(p + 28) - '0'; } -static void print(char* cs) { - for (int i = 0; i < 8; i++) { - auto c = cs[i]; - printf("%c", c); +//static void print(char* cs) { +// for (int i = 0; i < 8; i++) { +// auto c = cs[i]; +// printf("%c", c); +// } +// printf("\n"); +//} + +int action(const char* p) { + int i = 6; + if (*p == 's' && *(p + 5) == 'p') { + i = 0; + } + if (*p == 's' && *(p + 5) == 'l') { + i = 1; } - printf("\n"); + if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'r') { + i = 2; + } + if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'l') { + i = 2; + } + if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'b') { + i = 3; + } + if (*p == 'r' && *(p + 1) == 'e') { + i = 4; + } + if (*p == 'm') { + i = 5; + } + return i; } -std::pair<int64_t, int64_t> day21(line_view file) { +void day21(line_view file, char cs[8], char xs[8]) { struct { parse pf; scramble sf; - } fs[] = {{parse_swap, swap}, {parse_swap_char, swap_char}, - {parse_rotate, rotate}, {parse_rotate_char, rotate_char}, - {parse_reverse, reverse}, {parse_move, move}}; + scramble uf; + } fs[] = {{parse_swap, swap, swap}, + {parse_swap_char, swap_char, swap_char}, + {parse_rotate, rotate, undo_rotate}, + {parse_rotate_char, rotate_char, undo_rotate_char}, + {parse_reverse, reverse, reverse}, + {parse_move, move, undo_move}}; + + std::vector<line_view> vs; + // char cs[8] = {'g', 'd', 'f', 'c', 'a', 'b', 'e', 'h'}; - char cs[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; per_line( file, - [fs](line_view lv, char cs[8]) { + [fs](line_view lv, char cs[8], std::vector<line_view>& vs) { + vs.emplace_back(lv); const char* p = lv.line; int ds[2] = {0, 0}; - int i = 6; - if (*p == 's' && *(p + 5) == 'p') { - i = 0; - } - if (*p == 's' && *(p + 5) == 'l') { - i = 1; - } - if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'r') { - i = 2; - } - if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'l') { - i = 2; - } - if (*p == 'r' && *(p + 1) == 'o' && *(p + 7) == 'b') { - i = 3; - } - if (*p == 'r' && *(p + 1) == 'e') { - i = 4; - } - if (*p == 'm') { - i = 5; - } + int i = action(p); fs[i].pf(p, ds); fs[i].sf(cs, ds[0], ds[1]); - print(cs); + // print(cs); return true; }, - cs); - - print(cs); - return {0, 0}; + cs, vs); + + // char xs[8] = {'b', 'd', 'f', 'h', 'g', 'e', 'c', 'a'}; + // print(xs); + for (size_t i = 0; i < vs.size(); i++) { + size_t ii = vs.size() - i - 1; + const char* p = vs[ii].line; + int ds[2] = {0, 0}; + int x = action(p); + fs[x].pf(p, ds); + fs[x].uf(xs, ds[0], ds[1]); + // print(xs); + } } } // namespace aoc2016 diff --git a/src/2016/day21/aoc.h b/src/2016/day21/aoc.h index 008e844..65999b9 100644 --- a/src/2016/day21/aoc.h +++ b/src/2016/day21/aoc.h @@ -3,5 +3,5 @@ #include <vector> namespace aoc2016 { -std::pair<int64_t, int64_t> day21(line_view); +void day21(line_view, char cs[8], char xs[8]); } diff --git a/test/test_2016.cpp b/test/test_2016.cpp index d2dec77..cc3fcfd 100644 --- a/test/test_2016.cpp +++ b/test/test_2016.cpp @@ -188,9 +188,11 @@ TEST_CASE("Firewall Rules", "[2016]") { TEST_CASE("", "[2016]") { line_view lv = load_file("../src/2016/day21/input"); - auto p = aoc2016::day21(lv); - REQUIRE(0 == p.first); - REQUIRE(0 == p.second); + char cs[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + char xs[8] = {'f', 'b', 'g', 'd', 'c', 'e', 'a', 'h'}; + aoc2016::day21(lv,cs, xs); + REQUIRE(memcmp(cs, "bdfhgeca", 8) == 0); + REQUIRE(memcmp(xs, "gdfcabeh", 8) == 0); } |