aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-26 21:52:01 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-26 21:52:01 +0800
commit936a3bbbb950f35ec37ab2db2964c41883cba254 (patch)
treefd7d3b2aa943835ba2c565692b28813afc890fe3
parentffbb27b62cf714e254620be66835efc02e2ba1d3 (diff)
downloadadvent-of-code-936a3bbbb950f35ec37ab2db2964c41883cba254.tar.gz
advent-of-code-936a3bbbb950f35ec37ab2db2964c41883cba254.zip
2016 day22 part1
-rw-r--r--src/2016/day22/README.md85
-rw-r--r--src/2016/day22/aoc.cpp39
-rw-r--r--src/2016/day22/aoc.h36
-rw-r--r--src/2016/day22/input1
-rw-r--r--test/test_2016.cpp2
5 files changed, 159 insertions, 4 deletions
diff --git a/src/2016/day22/README.md b/src/2016/day22/README.md
index a611359..cb1406a 100644
--- a/src/2016/day22/README.md
+++ b/src/2016/day22/README.md
@@ -19,3 +19,88 @@ To do this, you'd like to count the number of viable pairs of nodes. A viable pa
How many viable pairs of nodes are there?
+--- Part Two ---
+
+Now that you have a better understanding of the grid, it's time to get to work.
+
+Your goal is to gain access to the data which begins in the node with y=0 and the highest x (that is, the node in the top-right corner).
+
+For example, suppose you have the following grid:
+
+Filesystem Size Used Avail Use%
+/dev/grid/node-x0-y0 10T 8T 2T 80%
+/dev/grid/node-x0-y1 11T 6T 5T 54%
+/dev/grid/node-x0-y2 32T 28T 4T 87%
+/dev/grid/node-x1-y0 9T 7T 2T 77%
+/dev/grid/node-x1-y1 8T 0T 8T 0%
+/dev/grid/node-x1-y2 11T 7T 4T 63%
+/dev/grid/node-x2-y0 10T 6T 4T 60%
+/dev/grid/node-x2-y1 9T 8T 1T 88%
+/dev/grid/node-x2-y2 9T 6T 3T 66%
+
+In this example, you have a storage grid 3 nodes wide and 3 nodes tall. The node you can access directly, node-x0-y0, is almost full. The node containing the data you want to access, node-x2-y0 (because it has y=0 and the highest x value), contains 6 terabytes of data - enough to fit on your node, if only you could make enough space to move it there.
+
+Fortunately, node-x1-y1 looks like it has enough free space to enable you to move some of this data around. In fact, it seems like all of the nodes have enough space to hold any node's data (except node-x0-y2, which is much larger, very full, and not moving any time soon). So, initially, the grid's capacities and connections look like this:
+
+( 8T/10T) -- 7T/ 9T -- [ 6T/10T]
+ | | |
+ 6T/11T -- 0T/ 8T -- 8T/ 9T
+ | | |
+ 28T/32T -- 7T/11T -- 6T/ 9T
+
+The node you can access directly is in parentheses; the data you want starts in the node marked by square brackets.
+
+In this example, most of the nodes are interchangable: they're full enough that no other node's data would fit, but small enough that their data could be moved around. Let's draw these nodes as .. The exceptions are the empty node, which we'll draw as _, and the very large, very full node, which we'll draw as #. Let's also draw the goal data as G. Then, it looks like this:
+
+(.) . G
+ . _ .
+ # . .
+
+The goal is to move the data in the top right, G, to the node in parentheses. To do this, we can issue some commands to the grid and rearrange the data:
+
+ Move data from node-y0-x1 to node-y1-x1, leaving node node-y0-x1 empty:
+
+ (.) _ G
+ . . .
+ # . .
+
+ Move the goal data from node-y0-x2 to node-y0-x1:
+
+ (.) G _
+ . . .
+ # . .
+
+ At this point, we're quite close. However, we have no deletion command, so we have to move some more data around. So, next, we move the data from node-y1-x2 to node-y0-x2:
+
+ (.) G .
+ . . _
+ # . .
+
+ Move the data from node-y1-x1 to node-y1-x2:
+
+ (.) G .
+ . _ .
+ # . .
+
+ Move the data from node-y1-x0 to node-y1-x1:
+
+ (.) G .
+ _ . .
+ # . .
+
+ Next, we can free up space on our node by moving the data from node-y0-x0 to node-y1-x0:
+
+ (_) G .
+ . . .
+ # . .
+
+ Finally, we can access the goal data by moving the it from node-y0-x1 to node-y0-x0:
+
+ (G) _ .
+ . . .
+ # . .
+
+So, after 7 steps, we've accessed the data we want. Unfortunately, each of these moves takes time, and we need to be efficient:
+
+What is the fewest number of steps required to move your goal data to node-x0-y0?
+
diff --git a/src/2016/day22/aoc.cpp b/src/2016/day22/aoc.cpp
index f115778..b4201e3 100644
--- a/src/2016/day22/aoc.cpp
+++ b/src/2016/day22/aoc.cpp
@@ -1,6 +1,43 @@
#include "aoc.h"
+#include <algorithm>
namespace aoc2016 {
-std::pair<int64_t, int64_t> day22(line_view) { return {0, 0}; }
+// A, B
+bool is_pair(const grid_node& n1, const grid_node& n2) { return n1.s[1] > 0 && n2.s[2] >= n1.s[1]; }
+
+void find_pairs(size_t x, const std::vector<grid_node>& ns, int* count) {
+ if (x < ns.size() - 1) {
+ auto& n1 = ns[x];
+ for (size_t i = x + 1; i < ns.size(); i++) {
+ auto& n2 = ns[i];
+ if (is_pair(n1, n2)) {
+ *count += 1;
+ }
+ if (is_pair(n2, n1)) {
+ *count += 1;
+ }
+ }
+ find_pairs(x + 1, ns, count);
+ }
+}
+
+std::pair<int64_t, int64_t> day22(line_view file) {
+ std::vector<grid_node> ns;
+
+ per_line(file, [&ns](line_view lv) {
+ if (*lv.line == '/') {
+ ns.emplace_back(lv);
+ }
+ return true;
+ });
+
+ // std::sort(ns.begin(), ns.end());
+ // for (auto& n : ns) {
+ // n.print();
+ // }
+ int count{0};
+ find_pairs(0, ns, &count);
+ return {count, 0};
+}
} // namespace aoc2016
diff --git a/src/2016/day22/aoc.h b/src/2016/day22/aoc.h
index b0ad2ba..d368f22 100644
--- a/src/2016/day22/aoc.h
+++ b/src/2016/day22/aoc.h
@@ -3,5 +3,39 @@
#include <vector>
namespace aoc2016 {
+
+struct grid_node {
+ int x = 0;
+ int y = 0;
+ int s[4] = {0}; // size, used, avail, use%
+
+ void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *pp = p;
+ }
+
+ grid_node(line_view lv) {
+ int* ds[] = {&x, &y, s, s + 1, s + 2, s + 3};
+ const char* p = lv.line;
+ int i{0};
+ while (p < lv.line + lv.length) {
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, ds[i]);
+ i++;
+ }
+ p++;
+ }
+ }
+
+ void print() const noexcept { printf("(%d,%d) %d %d %d %d\n", x, y, s[0], s[1], s[2], s[3]); }
+ friend bool operator<(const grid_node& n1, const grid_node& n2) {
+ return n1.s[2] > n2.s[2] ? true : n1.s[2] < n2.s[2] ? false : n1.s[0] > n2.s[0];
+ }
+};
+
std::pair<int64_t, int64_t> day22(line_view);
-}
+} // namespace aoc2016
diff --git a/src/2016/day22/input b/src/2016/day22/input
index ffcc4a6..6da000d 100644
--- a/src/2016/day22/input
+++ b/src/2016/day22/input
@@ -1023,4 +1023,3 @@ Filesystem Size Used Avail Use%
/dev/grid/node-x32-y28 92T 72T 20T 78%
/dev/grid/node-x32-y29 88T 65T 23T 73%
/dev/grid/node-x32-y30 85T 65T 20T 76%
-
diff --git a/test/test_2016.cpp b/test/test_2016.cpp
index dfbb462..092ad7a 100644
--- a/test/test_2016.cpp
+++ b/test/test_2016.cpp
@@ -199,7 +199,7 @@ TEST_CASE("Scrambled Letters and Hash", "[2016]") {
TEST_CASE("Grid Computing", "[2016]") {
line_view lv = load_file("../src/2016/day22/input");
auto p = aoc2016::day22(lv);
- REQUIRE(0 == p.first);
+ REQUIRE(990 == p.first);
REQUIRE(0 == p.second);
}