diff options
Diffstat (limited to 'src/2022/day13')
-rw-r--r-- | src/2022/day13/README.md | 33 | ||||
-rw-r--r-- | src/2022/day13/aoc.cpp | 40 | ||||
-rw-r--r-- | src/2022/day13/aoc.h | 35 | ||||
-rw-r--r-- | src/2022/day13/input | 3 | ||||
-rw-r--r-- | src/2022/day13/input0 | 3 |
5 files changed, 105 insertions, 9 deletions
diff --git a/src/2022/day13/README.md b/src/2022/day13/README.md index 9e192bc..d2bd84b 100644 --- a/src/2022/day13/README.md +++ b/src/2022/day13/README.md @@ -106,3 +106,36 @@ Using these rules, you can determine which of the pairs in the example are in th What are the indices of the pairs that are already in the right order? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is 13. Determine which pairs of packets are already in the right order. What is the sum of the indices of those pairs? + +--- Part Two --- +Now, you just need to put all of the packets in the right order. Disregard the blank lines in your list of received packets. + +The distress signal protocol also requires that you include two additional divider packets: + +[[2]] +[[6]] +Using the same rules as before, organize all packets - the ones in your list of received packets as well as the two divider packets - into the correct order. + +For the example above, the result of putting the packets in the correct order is: + +[] +[[]] +[[[]]] +[1,1,3,1,1] +[1,1,5,1,1] +[[1],[2,3,4]] +[1,[2,[3,[4,[5,6,0]]]],8,9] +[1,[2,[3,[4,[5,6,7]]]],8,9] +[[1],4] +[[2]] +[3] +[[4,4],4,4] +[[4,4],4,4,4] +[[6]] +[7,7,7] +[7,7,7,7] +[[8,7,6]] +[9] +Afterward, locate the divider packets. To find the decoder key for this distress signal, you need to determine the indices of the two divider packets and multiply them together. (The first packet is at index 1, the second packet is at index 2, and so on.) In this example, the divider packets are 10th and 14th, and so the decoder key is 140. + +Organize all of the packets into the correct order. What is the decoder key for the distress signal? diff --git a/src/2022/day13/aoc.cpp b/src/2022/day13/aoc.cpp index fb71da4..f844116 100644 --- a/src/2022/day13/aoc.cpp +++ b/src/2022/day13/aoc.cpp @@ -1,17 +1,44 @@ #include "aoc.h" #include <iostream> +#include <algorithm> + namespace aoc2022 { +struct pkt { + packet* p; + size_t i; +}; + +std::pair<int, int> find(const std::vector<pkt>& ps) { + size_t s1 = ps.size() - 1; + size_t s2 = ps.size() - 2; + size_t a{0}, b{0}; + for(size_t i = 0; i < ps.size(); i++) { + if (ps[i].i == s1) a = i; + if (ps[i].i == s2) b = i; + printf("%zu :", i); + ps[i].p->print(0); + printf("\n"); + } + printf("%zu %zu\n", a, b); + return {a + 1, b + 1}; +} + std::pair<int, int> day13(line_view file) { int count{0}; int pair{0}; + + + std::vector<pkt> all; packet* ps[2] = {nullptr, nullptr}; - per_line(file, [&pair, &count, &ps](line_view lv){ + per_line(file, [&pair, &count, &ps, &all](line_view lv){ if (lv.length > 1) { int i = pair % 2; - ps[i] = new packet; + packet* pkt = new packet; + ps[i] = pkt; const char* p = lv.line; packet::load(&p, &ps[i]); + all.push_back({pkt, (size_t) pair}); if (i == 1 && *(ps[0]) < *(ps[1])) { printf("group[%d] is in right order\n", pair/2 + 1); count += pair / 2 + 1; @@ -20,7 +47,14 @@ std::pair<int, int> day13(line_view file) { } return true; }); - return {count, 0}; + + std::sort(all.begin(), all.end(), [](const pkt& p1, const pkt& p2){ + return *(p1.p) < *(p2.p); + }); + + auto p = find(all); + + return {count, p.first * p.second}; } } diff --git a/src/2022/day13/aoc.h b/src/2022/day13/aoc.h index 7999e25..bfec96e 100644 --- a/src/2022/day13/aoc.h +++ b/src/2022/day13/aoc.h @@ -28,7 +28,7 @@ struct packet { static void load(const char** p, packet** pp) { const char* p0 = *p; - while (*p0 != '\n') { + while (*p0 != ']') { p0++; if (*p0 >= '0' && *p0 <= '9') { componet* c = new componet; @@ -40,14 +40,37 @@ struct packet { componet* c = new componet; c->t = is_list; c->p = new packet; - load(&p0, &c->p); (*pp)->ps.push_back(c); + load(&p0, &c->p); + } + } + *p = p0 + 1; + } + + char* indent (int s) { + static char space[100] = {0}; + memset(space, 0 ,100); + for(int i = 0; i < s; i++) { + space[i] = ' '; + } + return space; + }; + + void print(int d) { + printf("["); + int i{0}; + const char* s[] = {"", ","}; + for(componet* c : ps) { + if (c->t == is_int) { + printf("%s%d", s[(int) i > 0], c->v); + } + else { + printf("%s", s[(int) i > 0]); + c->p->print(d+1); } - else if (*p0 == ']') { - *p = p0; - return; - } + i++; } + printf("]"); } static packet* make_packet(int v) { diff --git a/src/2022/day13/input b/src/2022/day13/input index 165bc5f..03b9ba0 100644 --- a/src/2022/day13/input +++ b/src/2022/day13/input @@ -448,3 +448,6 @@ [[[[3,3,2,6],[],4,4,[9]]],[4,9],[3,[[5,2,10],[]]],[[[6,5],0,[]]],[[[5,4,8]],9,1]] [[],[[6,0]]] +[[6]] +[[2]] + diff --git a/src/2022/day13/input0 b/src/2022/day13/input0 index 545cddd..9c9b7a5 100644 --- a/src/2022/day13/input0 +++ b/src/2022/day13/input0 @@ -22,3 +22,6 @@ [1,[2,[3,[4,[5,6,7]]]],8,9] [1,[2,[3,[4,[5,6,0]]]],8,9] +[[6]] +[[2]] + |